{"id":1935,"date":"2020-07-29T21:21:21","date_gmt":"2020-07-29T13:21:21","guid":{"rendered":"http:\/\/39.97.231.10\/?p=1935"},"modified":"2020-07-29T21:21:21","modified_gmt":"2020-07-29T13:21:21","slug":"mysql-countcount1countfield%e5%8c%ba%e5%88%ab%e3%80%81%e6%80%a7%e8%83%bd%e5%b7%ae%e5%bc%82%e5%8f%8a%e4%bc%98%e5%8c%96%e5%bb%ba%e8%ae%ae","status":"publish","type":"post","link":"http:\/\/www.weijing.co\/?p=1935","title":{"rendered":"MySQL count(*),count(1),count(field)\u533a\u522b\u3001\u6027\u80fd\u5dee\u5f02\u53ca\u4f18\u5316\u5efa\u8bae"},"content":{"rendered":"<div>\n<div>\ncount\u51fd\u6570\u662f\u7528\u6765\u83b7\u53d6\u8868\u4e2d\u6ee1\u8db3\u4e00\u5b9a\u6761\u4ef6\u7684\u8bb0\u5f55\u6570\uff0c\u5e38\u89c1\u7528\u6cd5\u6709\u4e09\u79cd\uff0ccount(*),count(1),count(field)\uff0c\u8fd9\u4e09\u79cd\u6709\u4ec0\u4e48\u533a\u522b\uff1f\u5728\u6027\u80fd\u4e0a\u6709\u4f55\u5dee\u5f02\uff1f\u672c\u6587\u5c06\u901a\u8fc7\u6d4b\u8bd5\u6848\u4f8b\u8be6\u7ec6\u4ecb\u7ecd\u548c\u5206\u6790\u3002\n<\/div>\n<\/div>\n<div>\n<div>\n\u4e09\u8005\u6709\u4f55\u533a\u522b\uff1a<\/p>\n<ul>\n<li>count(field)\u4e0d\u5305\u542b\u5b57\u6bb5\u503c\u4e3aNULL\u7684\u8bb0\u5f55\u3002<\/li>\n<li>count(*)\u5305\u542bNULL\u8bb0\u5f55\u3002<\/li>\n<li>select(*)\u4e0eselect(1) \u5728InnoDB\u4e2d\u6027\u80fd\u6ca1\u6709\u4efb\u4f55\u533a\u522b\uff0c\u5904\u7406\u65b9\u5f0f\u76f8\u540c\u3002\u5b98\u65b9\u6587\u6863\u63cf\u8ff0\u5982\u4e0b\uff1a<br \/>\nInnoDB\u00a0handles\u00a0SELECT COUNT(*)\u00a0and\u00a0SELECT COUNT(1)\u00a0operations in the same way. There is no performance difference.<\/li>\n<\/ul>\n<h5>1. \u6027\u80fd\u5bf9\u6bd4<\/h5>\n<p>\u901a\u8fc7\u6848\u4f8b\u6765\u6d4b\u8bd5\u4e00\u4e0bcount(*)\uff0ccount(1)\uff0ccount(field)\u7684\u6027\u80fd\u5dee\u5f02\uff0cMySQL\u7248\u672c\u4e3a5.7.19\uff0c\u6d4b\u8bd5\u8868\u662f\u4e00\u5f20sysbench\u751f\u6210\u7684\u8868\uff0c\u8868\u540dsbtest1\uff0c\u603b\u8bb0\u5f55\u65702411645\uff0c\u5982\u4e0b\uff1a<\/p>\n<pre>CREATE TABLE sbtest1 (\nid int(11) NOT NULL AUTO_INCREMENT,\nk int(11) DEFAULT NULL,\nc char(120) NOT NULL DEFAULT \\'\\',\npad char(60) NOT NULL DEFAULT \\'\\',\nPRIMARY KEY (id),\nKEY k_1 (k)\n) ENGINE=InnoDB;\n<\/pre>\n<p>\u6d4b\u8bd5SQL\u8bed\u53e5\uff1a<br \/>\nselect count(*) from sbtest1;<br \/>\nselect count(1) from sbtest1;<br \/>\nselect count(id) from sbtest1;<br \/>\nselect count(k) from sbtest1;<br \/>\nselect count(c) from sbtest1;<br \/>\nselect count(pad) from sbtest1;<br \/>\n\u9488\u5bf9count(*)\u3001count(1)\u548ccount(id)\uff0c\u52a0\u4e86\u5f3a\u5236\u8d70\u4e3b\u952e\u7684\u6d4b\u8bd5\uff0c\u5982\u4e0b\uff1a<br \/>\nselect count(*) from sbtest1 force index(primary);<br \/>\nselect count(1) from sbtest1 force index(primary);<br \/>\nselect count(id) from sbtest1 force index(primary);<br \/>\n\u53e6\u5916\u5bf9\u4e0d\u540c\u7684\u6d4b\u8bd5SQL\uff0c\u6536\u96c6\u4e86profile\uff0c\u53d1\u73b0\u4e3b\u8981\u8017\u65f6\u90fd\u5728Sending data\u8fd9\u4e2a\u9636\u6bb5\uff0c\u8bb0\u5f55Sending data\u503c\u3002<br \/>\n\u6c47\u603b\u6d4b\u8bd5\u7ed3\u679c\uff1a<\/p>\n<table>\n<thead>\n<tr>\n<th>\u7c7b\u578b<\/th>\n<th>\u8017\u65f6(s)<\/th>\n<th>\u7d22\u5f15<\/th>\n<th>Sending data\u8017\u65f6(s)<\/th>\n<\/tr>\n<\/thead>\n<tbody>\n<tr>\n<td>count(*)<\/td>\n<td>0.47<\/td>\n<td>k_1<\/td>\n<td>0.463624<\/td>\n<\/tr>\n<tr>\n<td>count(1)<\/td>\n<td>0.46<\/td>\n<td>k_1<\/td>\n<td>0.463242<\/td>\n<\/tr>\n<tr>\n<td>count(id)<\/td>\n<td>0.52<\/td>\n<td>k_1<\/td>\n<td>0.521618<\/td>\n<\/tr>\n<tr>\n<td>count(*)\u5f3a\u5236\u8d70\u4e3b\u952e<\/td>\n<td>0.54<\/td>\n<td>primay key<\/td>\n<td>0.538737<\/td>\n<\/tr>\n<tr>\n<td>count(1)\u5f3a\u5236\u8d70\u4e3b\u952e<\/td>\n<td>0.55<\/td>\n<td>primary key<\/td>\n<td>0.545007<\/td>\n<\/tr>\n<tr>\n<td>count(id)\u5f3a\u5236\u8d70\u4e3b\u952e<\/td>\n<td>0.60<\/td>\n<td>primary key<\/td>\n<td>0.598975<\/td>\n<\/tr>\n<tr>\n<td>count(k)<\/td>\n<td>0.53<\/td>\n<td>k_1<\/td>\n<td>0.529366<\/td>\n<\/tr>\n<tr>\n<td>count(c)<\/td>\n<td>0.81<\/td>\n<td>NULL<\/td>\n<td>0.813918<\/td>\n<\/tr>\n<tr>\n<td>count(pad)<\/td>\n<td>0.76<\/td>\n<td>NULL<\/td>\n<td>0.762040<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<p>\u7ed3\u679c\u5206\u6790\uff1a<\/p>\n<ol>\n<li>\u4ece\u4ee5\u4e0a\u6d4b\u8bd5\u7ed3\u679c\u6765\u770b\uff0ccount(*)\u548ccount(1)\u6027\u80fd\u57fa\u672c\u4e00\u6837\uff0c\u9ed8\u8ba4\u8d70\u4e8c\u7ea7\u7d22\u5f15(k_1)\uff0c\u6027\u80fd\u6700\u597d\uff0c\u8fd9\u4e5f\u9a8c\u8bc1\u4e86count(*)\u548ccount(1)\u5728InnoDB\u5185\u90e8\u5904\u7406\u65b9\u5f0f\u4e00\u6837\u3002<\/li>\n<li>count(id) \u867d\u7136\u4e5f\u8d70\u4e8c\u7ea7\u7d22\u5f15(k_1)\uff0c\u4f46\u662f\u6027\u80fd\u660e\u663e\u4f4e\u4e8ecount(*)\u548ccount(1)\uff0c\u53ef\u80fdMySQL\u5185\u90e8\u5728\u5904\u7406count(*)\u548ccount(1)\u65f6\u505a\u4e86\u989d\u5916\u7684\u4f18\u5316\u3002<\/li>\n<li>\u5f3a\u5236\u8d70\u4e3b\u952e\u7d22\u5f15\u65f6\uff0c\u6027\u80fd\u53cd\u800c\u6ca1\u6709\u8d70\u66f4\u5c0f\u7684\u4e8c\u7ea7\u7d22\u5f15\u597d\uff0cInnoDB\u5b58\u50a8\u5f15\u64ce\u662f\u7d22\u5f15\u7ec4\u7ec7\u8868\uff0c\u884c\u6570\u636e\u5728\u4e3b\u952e\u7d22\u5f15\u7684\u53f6\u5b50\u8282\u70b9\u4e0a\uff0c\u8d70\u4e3b\u952e\u7d22\u5f15\u626b\u63cf\u65f6\uff0c\u5904\u7406\u7684\u6570\u636e\u91cf\u6bd4\u4e8c\u7ea7\u7d22\u5f15\u66f4\u591a\uff0c\u6240\u4ee5\u6027\u80fd\u4e0d\u53ca\u4e8c\u7ea7\u7d22\u5f15\u3002<\/li>\n<li>count(c)\u548ccount(pad)\u6ca1\u6709\u8d70\u7d22\u5f15\uff0c\u6027\u80fd\u6700\u5dee\uff0c\u4f46\u662f\u660e\u663ecount(pad)\u6bd4count(c)\u597d\uff0c\u56e0\u4e3apad\u5b57\u6bb5\u7c7b\u578b\u4e3achar(60)\uff0c\u5c0f\u4e8e\u5b57\u6bb5c\u7684char(120)\uff0c\u5c3d\u7ba1\u4e24\u8005\u6027\u80fd\u57ab\u5e95\uff0c\u4f46\u662f\u5b57\u6bb5\u5c0f\u7684\u6027\u80fd\u76f8\u5bf9\u66f4\u597d\u4e9b\u3002<\/li>\n<\/ol>\n<h5>2. count(*)\u5ef6\u4f38<\/h5>\n<ul>\n<li>\u57285.7.18\u7248\u672c\u4e4b\u524d\uff0cInnoDB\u5904\u7406select count(*) \u662f\u901a\u8fc7\u626b\u63cf\u805a\u7c07\u7d22\u5f15\uff0c\u6765\u83b7\u53d6\u603b\u8bb0\u5f55\u6570\u3002<\/li>\n<li>\u4ece5.7.18\u7248\u672c\u5f00\u59cb\uff0cInnoDB\u626b\u63cf\u4e00\u4e2a\u6700\u5c0f\u7684\u53ef\u7528\u7684\u4e8c\u7ea7\u7d22\u5f15\u6765\u83b7\u53d6\u603b\u8bb0\u5f55\u6570\uff0c\u6216\u8005\u7531SQL hint\u6765\u544a\u8bc9\u4f18\u5316\u5668\u4f7f\u7528\u54ea\u4e2a\u7d22\u5f15\u3002\u5982\u679c\u4e8c\u7ea7\u7d22\u5f15\u4e0d\u5b58\u5728\uff0cInnoDB\u5c06\u4f1a\u626b\u63cf\u805a\u7c07\u7d22\u5f15\u3002<\/li>\n<\/ul>\n<p>\u6267\u884cselect count(*)\u5728\u5927\u90e8\u5206\u573a\u666f\u4e0b\u6027\u80fd\u90fd\u4e0d\u4f1a\u592a\u597d\uff0c\u5c24\u5176\u662f\u8868\u8bb0\u5f55\u6570\u7279\u522b\u5927\u7684\u60c5\u51b5\u4e0b\uff0c\u7d22\u5f15\u6570\u636e\u4e0d\u5728buffer pool\u91cc\u9762\uff0c\u9700\u8981\u9891\u7e41\u7684\u8bfb\u78c1\u76d8\uff0c\u6027\u80fd\u5c06\u66f4\u5dee\u3002<\/p>\n<h5>3. count(*)\u4f18\u5316\u601d\u8def<\/h5>\n<ol>\n<li>\u4e00\u79cd\u4f18\u5316\u65b9\u6cd5\uff0c\u662f\u4f7f\u7528\u4e00\u4e2a\u7edf\u8ba1\u8868\u6765\u5b58\u50a8\u8868\u7684\u8bb0\u5f55\u603b\u6570\uff0c\u5728\u6267\u884cDML\u64cd\u4f5c\u65f6\uff0c\u540c\u65f6\u66f4\u65b0\u8be5\u7edf\u8ba1\u8868\u3002\u8fd9\u79cd\u65b9\u6cd5\u9002\u7528\u4e8e\u66f4\u65b0\u8f83\u5c11\uff0c\u8bfb\u8f83\u591a\u7684\u573a\u666f\uff0c\u800c\u5bf9\u4e8e\u9ad8\u5e76\u53d1\u5199\u64cd\u4f5c\uff0c\u6027\u80fd\u6709\u5f88\u5927\u5f71\u54cd\uff0c\u56e0\u4e3a\u9700\u8981\u5e76\u53d1\u66f4\u65b0\u70ed\u70b9\u8bb0\u5f55\u3002<\/li>\n<li>\u5982\u679c\u4e1a\u52a1\u5bf9count\u6570\u91cf\u7684\u7cbe\u5ea6\u6ca1\u6709\u592a\u5927\u8981\u6c42\uff0c\u53ef\u4f7f\u7528show table status\u4e2d\u7684\u884c\u6570\u4f5c\u4e3a\u8fd1\u4f3c\u503c\u3002<\/li>\n<\/ol>\n<\/div>\n<\/div>\n","protected":false},"excerpt":{"rendered":"<p>count\u51fd\u6570\u662f\u7528\u6765\u83b7\u53d6\u8868\u4e2d\u6ee1\u8db3\u4e00\u5b9a\u6761\u4ef6\u7684\u8bb0\u5f55\u6570\uff0c\u5e38\u89c1\u7528\u6cd5\u6709\u4e09\u79cd\uff0ccount(*),count(1),coun &hellip;<\/p>\n<p class=\"read-more\"> <a class=\"\" href=\"http:\/\/www.weijing.co\/?p=1935\"> <span class=\"screen-reader-text\">MySQL count(*),count(1),count(field)\u533a\u522b\u3001\u6027\u80fd\u5dee\u5f02\u53ca\u4f18\u5316\u5efa\u8bae<\/span> \u67e5\u770b\u5168\u6587 &raquo;<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":""},"categories":[90,88],"tags":[],"uagb_featured_image_src":{"full":false,"thumbnail":false,"medium":false,"medium_large":false,"large":false,"1536x1536":false,"2048x2048":false,"mailpoet_newsletter_max":false,"woocommerce_thumbnail":false,"woocommerce_single":false,"woocommerce_gallery_thumbnail":false,"shop_catalog":false,"shop_single":false,"shop_thumbnail":false},"uagb_author_info":{"display_name":"weijing","author_link":"http:\/\/www.weijing.co\/?author=4"},"uagb_comment_info":0,"uagb_excerpt":"count\u51fd\u6570\u662f\u7528\u6765\u83b7\u53d6\u8868\u4e2d\u6ee1\u8db3\u4e00\u5b9a\u6761\u4ef6\u7684\u8bb0\u5f55\u6570\uff0c\u5e38\u89c1\u7528\u6cd5\u6709\u4e09\u79cd\uff0ccount(*),count(1),coun&hellip;","_links":{"self":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1935"}],"collection":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1935"}],"version-history":[{"count":0,"href":"http:\/\/www.weijing.co\/index.php?rest_route=\/wp\/v2\/posts\/1935\/revisions"}],"wp:attachment":[{"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1935"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1935"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/www.weijing.co\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1935"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}