{"id":3075,"date":"2021-01-07T14:05:09","date_gmt":"2021-01-07T06:05:09","guid":{"rendered":"https:\/\/www.gbase8.cn\/?p=3075"},"modified":"2021-07-19T15:50:59","modified_gmt":"2021-07-19T07:50:59","slug":"gbase-8a%e9%87%8ccount%e3%80%81count1%e5%92%8ccount%e5%ad%97%e6%ae%b5%e7%9a%84%e5%8c%ba%e5%88%ab","status":"publish","type":"post","link":"https:\/\/www.gbase8.cn\/en\/3075","title":{"rendered":"\u6570\u636e\u5e93SQL\u9762\u8bd5\u9898\uff1a\u6570\u636e\u5e93SQL\u7684count(*)\u3001count(1)\u548ccount(\u5b57\u6bb5)\u7684\u533a\u522b\u548c\u6027\u80fd\u5dee\u5f02"},"content":{"rendered":"<p>\u5728\u7edf\u8ba1\u884c\u6570\u65f6\uff0c\u5e38\u7528\u7684\u662fcount(*)\u3002\u800c\u5728\u65e9\u671f\u7684\u6570\u636e\u5e93\uff0c\u8fd9\u4e2a*\u9ed8\u8ba4\u4f1a\u88ab\u89e3\u6790\uff0c\u5728\u6210\u5c11\u8bb8\u7684\u6d6a\u8d39\uff0c\u6240\u4ee5\u90fd\u5efa\u8bae\u5199\u6210count(1)\uff0c\u800c\u9488\u5bf9count(\u5b57\u6bb5\uff09\u8fd9\u4e2a\u5199\u6cd5\u5e76\u4e0d\u5e38\u89c1\u3002\u672c\u6587\u4ee5GBase 8a\u6570\u636e\u5e93\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u4ed6\u4eec\u4e4b\u95f4\u7684\u533a\u522b\u3002<\/p>\n\n\n\n<div id=\"ez-toc-container\" class=\"ez-toc-v2_0_84 counter-hierarchy ez-toc-counter ez-toc-grey ez-toc-container-direction\">\n<p class=\"ez-toc-title\" style=\"cursor:inherit\">Directory Navigation<\/p>\n<label for=\"ez-toc-cssicon-toggle-item-6a2af858530c5\" class=\"ez-toc-cssicon-toggle-label\"><span class=\"\"><span class=\"eztoc-hide\" style=\"display:none;\">Toggle<\/span><span class=\"ez-toc-icon-toggle-span\"><svg style=\"fill: #999;color:#999\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" class=\"list-377408\" width=\"20px\" height=\"20px\" viewbox=\"0 0 24 24\" fill=\"none\"><path d=\"M6 6H4v2h2V6zm14 0H8v2h12V6zM4 11h2v2H4v-2zm16 0H8v2h12v-2zM4 16h2v2H4v-2zm16 0H8v2h12v-2z\" fill=\"currentColor\"><\/path><\/svg><svg style=\"fill: #999;color:#999\" class=\"arrow-unsorted-368013\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" width=\"10px\" height=\"10px\" viewbox=\"0 0 24 24\" version=\"1.2\" baseprofile=\"tiny\"><path d=\"M18.2 9.3l-6.2-6.3-6.2 6.3c-.2.2-.3.4-.3.7s.1.5.3.7c.2.2.4.3.7.3h11c.3 0 .5-.1.7-.3.2-.2.3-.5.3-.7s-.1-.5-.3-.7zM5.8 14.7l6.2 6.3 6.2-6.3c.2-.2.3-.5.3-.7s-.1-.5-.3-.7c-.2-.2-.4-.3-.7-.3h-11c-.3 0-.5.1-.7.3-.2.2-.3.5-.3.7s.1.5.3.7z\"\/><\/svg><\/span><\/span><\/label><input type=\"checkbox\"  id=\"ez-toc-cssicon-toggle-item-6a2af858530c5\"  aria-label=\"Toggle\" \/><nav><ul class='ez-toc-list ez-toc-list-level-1' ><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-1\" href=\"https:\/\/www.gbase8.cn\/en\/3075\/#%E6%A0%B7%E4%BE%8B%E6%95%B0%E6%8D%AE\" >\u6837\u4f8b\u6570\u636e<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-2\" href=\"https:\/\/www.gbase8.cn\/en\/3075\/#coun_%E5%92%8C_count1\" >coun(*) \u548c count(1)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-3\" href=\"https:\/\/www.gbase8.cn\/en\/3075\/#count%E5%AD%97%E6%AE%B5\" >count(\u5b57\u6bb5)<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-4\" href=\"https:\/\/www.gbase8.cn\/en\/3075\/#%E6%80%A7%E8%83%BD%E5%B7%AE%E5%BC%82\" >\u6027\u80fd\u5dee\u5f02<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-5\" href=\"https:\/\/www.gbase8.cn\/en\/3075\/#%E6%80%BB%E7%BB%93\" >Summary<\/a><\/li><li class='ez-toc-page-1 ez-toc-heading-level-2'><a class=\"ez-toc-link ez-toc-heading-6\" href=\"https:\/\/www.gbase8.cn\/en\/3075\/#%E5%8F%82%E8%80%83\" >\u53c2\u8003<\/a><\/li><\/ul><\/nav><\/div>\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%A0%B7%E4%BE%8B%E6%95%B0%E6%8D%AE\"><\/span>\u6837\u4f8b\u6570\u636e<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u6837\u4f8b\u6570\u636e\u51713\u884c\uff0c2\u5217\u3002\u5176\u4e2did\u5217\u662f\u6570\u5b57\uff0c\u5168\u90e8\u7531\u503c\u3002name\u91cc\u662f\u5b57\u7b26\u4e32\uff0c\u6709\u6570\u636e\u6ca1\u6709\u586b\u5199\uff0c\u4e3aNULL\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase&gt; select * from tt1;\n+------+-------+\n| id   | name  |\n+------+-------+\n|    1 | First |\n|    2 |       |\n|    3 | NULL  |\n+------+-------+\n3 rows in set (Elapsed: 00:00:00.00)\n<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"coun_%E5%92%8C_count1\"><\/span>coun(*) \u548c count(1)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u83b7\u5f97\u6240\u6709\u7684\u884c\u6570\u3002\u5f53\u7136\u7528count(2222)\u4e5f\u884c\uff0c\u8fd9\u91cc\u53ea\u662fcount\uff0c\u7edf\u8ba1\u884c\u6570\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase&gt; select count(*) from tt1;\n+----------+\n| count(*) |\n+----------+\n|        3 |\n+----------+\n1 row in set (Elapsed: 00:00:00.02)\n\ngbase&gt; select count(1) from tt1;\n+----------+\n| count(1) |\n+----------+\n|        3 |\n+----------+\n1 row in set (Elapsed: 00:00:00.01)\n\ngbase&gt; select count(222222) from tt1;\n+---------------+\n| count(222222) |\n+---------------+\n|             3 |\n+---------------+\n1 row in set (Elapsed: 00:00:00.01)\n<\/code><\/pre>\n\n\n\n<p>\u4ece\u7ed3\u679c\u770b\uff0c\u6ca1\u6709\u4efb\u4f55\u533a\u522b\u3002\u800c\u4e14\u5728\u73b0\u4eca\u4e3b\u6d41\u7684\u6570\u636e\u5e93\uff0c\u5bf9*\u90fd\u505a\u4e86\u4f18\u5316\uff0c\u4e0d\u518d\u505a\u89e3\u6790\u4e4b\u7c7b\u7684\u64cd\u4f5c\u4e86\uff0c\u4ece\u6027\u80fd\u89d2\u5ea6\u8bb2\uff0c\u4e5f\u548ccount(1)\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff0c\u53ef\u4ee5\u5927\u80c6\u7684\u7528\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"count%E5%AD%97%E6%AE%B5\"><\/span>count(\u5b57\u6bb5)<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u9488\u5bf9\u4e0d\u540c\u7684\u5b57\u6bb5\uff0c\u67e5\u8be2\u7ed3\u679c\u6709\u4e0d\u540c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>gbase&gt; select count(id) from tt1;\n+-----------+\n| count(id) |\n+-----------+\n|         3 |\n+-----------+\n1 row in set (Elapsed: 00:00:00.00)\n\ngbase&gt; select count(name) from tt1;\n+-------------+\n| count(name) |\n+-------------+\n|           2 |\n+-------------+\n1 row in set (Elapsed: 00:00:00.00)\n<\/code><\/pre>\n\n\n\n<p>\u53ef\u4ee5\u770b\u5230count(id)\u7ed3\u679c\u662f3\uff0c\u800ccount(name)\u7ed3\u679c\u4e3a2\u3002\u533a\u522b\u662fname\u91cc\u67091\u884c\u662fNULL\u503c\u3002<\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%80%A7%E8%83%BD%E5%B7%AE%E5%BC%82\"><\/span>\u6027\u80fd\u5dee\u5f02<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u6839\u636e\u7b2c\u4e09\u65b9\u7684\u6d4b\u8bd5\u7ed3\u679c\u5982\u4e0b\uff1a\u9664\u4e86PG,\u5176\u5b83\u6570\u636e\u5e93\u90fd\u65e0\u4efb\u4f55\u5f71\u54cd\u3002PG\u5efa\u8bae\u7528count(*)\u66f4\u5feb\u4e00\u4e9b\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>MySQL: Doesn\u2019t matter. Sometimes COUNT(1) was faster, sometimes COUNT(*) was faster, so all differences were only benchmark artifacts\r\nOracle: Doesn\u2019t matter. Like MySQL\r\nPostgreSQL: Does matter (!). COUNT(*) was consistently faster by around 10% on 1M rows, that\u2019s much more than I had expected\r\nSQL Server: Doesn\u2019t matter. Like MySQL<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E6%80%BB%E7%BB%93\"><\/span>Summary<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<p>\u5982\u679c\u6570\u636e\u6ca1\u6709NULL\u503c\uff0c\u90a3\u4e48\u4e09\u4e2a\u7528\u6cd5\u7684\u7ed3\u679c\u662f\u5b8c\u5168\u4e00\u6837\u7684\uff0c\u4f46\u76f8\u5bf9\u7684count(\u5b57\u6bb5)\u662f\u8981\u68c0\u67e5\u662f\u5426\u4e3aNULL\u503c\uff0c\u6027\u80fd\u4f1a\u7565\u6709\u635f\u5931\u3002<\/p>\n\n\n\n<p>\u5982\u679c\u6570\u636e\u91cc\u6709NULL\u503c\uff0c\u5219count(\u5b57\u6bb5)\u53ea\u8fd4\u56de\u975eNULL\u7684\u6570\u636e\u884c\u6570\uff0c\u4e0e\u5176\u5b832\u4e2a\u7ed3\u679c\u96c6\u662f\u4e0d\u540c\u7684\u3002<\/p>\n\n\n\n<p>\u6240\u4ee5\uff0c\u5982\u679c\u4e0d\u662f\u7279\u610f\u7edf\u8ba1\u975eNULL\u503c\uff0c\u4e0d\u5efa\u8bae\u7528count(\u5b57\u6bb5),\u800c\u662f\u7528count(*)\u6216count(1)\u4ee3\u66ff\u3002<\/p>\n\n\n\n<p><\/p>\n\n\n\n<h2 class=\"wp-block-heading\"><span class=\"ez-toc-section\" id=\"%E5%8F%82%E8%80%83\"><\/span>\u53c2\u8003<span class=\"ez-toc-section-end\"><\/span><\/h2>\n\n\n\n<figure class=\"wp-block-embed is-type-link is-provider-java-sql-and-jooq wp-block-embed-java-sql-and-jooq\"><div class=\"wp-block-embed__wrapper\">\n<a href=\"https:\/\/blog.jooq.org\/2019\/09\/19\/whats-faster-count-or-count1\/\">What&#8217;s Faster? COUNT(*) or&nbsp;COUNT(1)?<\/a>\n<\/div><\/figure>\n\n\n\n<p><\/p>","protected":false},"excerpt":{"rendered":"<p>\u5728\u7edf\u8ba1\u884c\u6570\u65f6\uff0c\u5e38\u7528\u7684\u662fcount(*)\u3002\u800c\u5728\u65e9\u671f\u7684\u6570\u636e\u5e93\uff0c\u8fd9\u4e2a*\u9ed8\u8ba4\u4f1a\u88ab\u89e3\u6790\uff0c\u5728\u6210\u5c11\u8bb8\u7684\u6d6a\u8d39\uff0c\u6240\u4ee5\u90fd\u5efa\u8bae\u5199\u6210count(1)\uff0c\u800c\u9488\u5bf9count(\u5b57\u6bb5\uff09\u8fd9\u4e2a\u5199\u6cd5\u5e76\u4e0d\u5e38\u89c1\u3002\u672c\u6587\u4ee5GBase 8a\u6570\u636e\u5e93\u4e3a\u4f8b\uff0c\u4ecb\u7ecd\u4ed6\u4eec\u4e4b\u95f4\u7684\u533a\u522b\u3002<\/p>","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4],"tags":[81],"class_list":["post-3075","post","type-post","status-publish","format-standard","hentry","category-gbase8a","tag-81"],"_links":{"self":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/3075","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/comments?post=3075"}],"version-history":[{"count":8,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/3075\/revisions"}],"predecessor-version":[{"id":7564,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/posts\/3075\/revisions\/7564"}],"wp:attachment":[{"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/media?parent=3075"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/categories?post=3075"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.gbase8.cn\/en\/wp-json\/wp\/v2\/tags?post=3075"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}