楂樻€ц兘MySQL鈥斺€攃ount(*) 鍜?count鍜宑ount(鍒楀悕)鍖哄埆

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了楂樻€ц兘MySQL鈥斺€攃ount(*) 鍜?count鍜宑ount(鍒楀悕)鍖哄埆相关的知识,希望对你有一定的参考价值。

鏍囩锛?a href='http://www.mamicode.com/so/1/%e9%80%9a%e9%85%8d%e7%ac%a6' title='閫氶厤绗?>閫氶厤绗?/a>   rod   rom   绠€鍗曠殑   閬楃暀浠g爜   primary   鏌ヨ   鐩存帴   http   

楂樻€ц兘mysql鈥斺€攃ount(*) 鍜?count(1)鍜宑ount(鍒楀悕)鍖哄埆

鎽樿嚜:

https://cloud.tencent.com/developer/article/1401567

https://mp.weixin.qq.com/s/MCFHNOQnTtJ6MGVjM3DP4A

鎵ц鏁堟灉涓婏細

  • count(*)鍖呮嫭浜嗘墍鏈夌殑鍒楋紝鐩稿綋浜庤鏁帮紝鍦ㄧ粺璁$粨鏋滅殑鏃跺€欙紝涓嶄細蹇界暐鍒楀€间负NULL
  • count(1)鍖呮嫭浜嗘墍鏈夊垪锛岀敤1浠h〃浠g爜琛岋紝鍦ㄧ粺璁$粨鏋滅殑鏃跺€欙紝涓嶄細蹇界暐鍒楀€间负NULL
  • count(鍒楀悕)鍙寘鎷垪鍚嶉偅涓€鍒楋紝鍦ㄧ粺璁$粨鏋滅殑鏃跺€欙紝浼氬拷鐣ュ垪鍊间负绌猴紙杩欓噷鐨勭┖涓嶆槸鍙┖瀛楃涓叉垨鑰?锛岃€屾槸琛ㄧずnull锛夌殑璁℃暟锛屽嵆鏌愪釜瀛楁鍊间负NULL鏃讹紝涓嶇粺璁°€?/li>

鎵ц鏁堢巼涓婏細

  • 鍒楀悕涓轰富閿紝count(1)浼氭瘮count(鍒楀悕)蹇?/li>
  • 鍒楀悕涓嶄负涓婚敭锛宑ount(1)浼氭瘮count(鍒楀悕)蹇?/li>
  • 濡傛灉琛ㄥ涓垪骞朵笖娌℃湁涓婚敭锛屽垯 count(1) 鐨勬墽琛屾晥鐜囦紭浜?count(*)
  • 濡傛灉鏈変富閿紝鍒?select count锛堜富閿級鐨勬墽琛屾晥鐜囨槸鏈€浼樼殑
  • 濡傛灉琛ㄥ彧鏈変竴涓瓧娈碉紝鍒?select count(*) 鏈€浼樸€?/li>

count(鍒楀悕)鏌愪釜瀛楁鍊间负NULL鏃讹紝涓嶇粺璁?/p>

濡傛灉闂竴涓▼搴忓憳MySQL涓璖ELECT COUNT(1)鍜孲ELECT COUNT()鏈変粈涔堝尯鍒紝浼氭湁寰堝浜虹粰鍑鸿繖鏍风殑绛旀鈥淪ELECT COUNT()鈥濇渶缁堜細杞寲鎴愨€淪ELECT COUNT(1)锛岃€孲ELECT COUNT(1)鐪佺暐浜嗚浆鎹㈢殑杩欎竴姝ワ紝鎵€浠ELECT COUNT(1)鏁堢巼鏇撮珮鈥滐紝鐢氳嚦鏈変竴浜涢潰璇曞畼涔熶細缁欏嚭绫讳技鐨勭瓟妗堛€傛渶杩戝湪鐪嬩竴浜涘巻鍙查仐鐣欎唬鐮侊紝缁濆ぇ澶氭暟缁熻鏁伴噺鐨凷QL閮藉湪鐢⊿ELECT COUNT(1)锛岃寰楁湁蹇呰鎼炴竻妤氳繖涓棶棰樸€?/p>

棣栧厛锛屼互鎴戜滑鏈€甯歌鐨勪袱绉嶆暟鎹簱琛ㄥ紩鎿嶮yISAM鍜孖nnodb鏉ヨ銆?/p>

MyISAM

MyISAM鍦ㄧ粺璁¤〃鐨勬€昏鏁扮殑鏃跺€欎細寰堝揩锛屼絾鏄湁涓ぇ鍓嶆彁锛?strong>涓嶈兘鍔犳湁浠讳綍WHERE鏉′欢銆傝繖鏄洜涓猴細MyISAM瀵逛簬琛ㄧ殑琛屾暟鍋氫簡浼樺寲锛屽叿浣撳仛娉曟槸鏈変竴涓彉閲忓瓨鍌ㄤ簡琛ㄧ殑琛屾暟锛屽鏋滄煡璇㈡潯浠舵病鏈塛HERE鏉′欢鍒欐槸鏌ヨ琛ㄤ腑涓€鍏辨湁澶氬皯鏉℃暟鎹紝MyISAM鍙互鍋氬埌杩呴€熻繑鍥烇紝鎵€浠ヤ篃瑙i噴浜嗗鏋滃姞WHERE鏉′欢锛屽垯璇ヤ紭鍖栧氨涓嶈捣浣滅敤浜嗐€傜粏蹇冪殑鍚屽浼氬彂鐜帮紝innodb鐨勮〃涔熸湁杩欎箞涓€涓瓨鍌ㄤ簡琛ㄨ鏁扮殑鍙橀噺锛屼絾鏄緢閬楁喚杩欎釜鍊兼槸涓€涓及璁″€硷紝娌℃湁浠€涔堝疄闄呮剰涔夈€?/p>

Innodb

鍦ㄨ寮曟搸涓嬶紝COUNT(1)鍜孋OUNT(*)鍝釜蹇憿锛熺粨璁烘槸锛氳繖淇╁湪楂樼増鏈殑MySQL锛?.5鍙婁互鍚庯紝5.1鐨勬病鏈夎€冭瘉锛夋槸娌℃湁浠€涔堝尯鍒殑锛屼篃灏辨病鏈塁OUN(1)浼氭瘮COUNT(*)鏇村揩杩欎竴璇翠簡銆?/p>

WHY锛熻繖灏辫浠嶤OUNT()鍑芥暟鐨勫叿浣撳惈涔夎璧蜂簡銆傗€?/p>

COUNT()鏈変袱涓潪甯镐笉鍚岀殑浣滅敤锛氬畠鍙互缁熻鏌愪釜鍒楀€肩殑鏁伴噺锛屼篃鍙互缁熻琛屾暟銆傚湪缁熻鍒楀€兼椂瑕佹眰鍒楀€兼槸闈炵┖鐨勶紙涓嶇粺璁ULL锛夈€傚鏋滃湪COUNT()鐨勬嫭鍙蜂腑瀹氫簡鍒楁垨鑰呭垪琛ㄨ揪寮忥紝鍒欑粺璁$殑灏辨槸杩欎釜琛ㄨ揪寮忔湁鍊肩殑缁撴灉鏁般€?.....COUNT()鐨勫彟涓€涓綔鐢ㄦ槸缁熻缁撴灉闆嗙殑琛屾暟銆傚綋MySQL纭鎷彿鍐呯殑琛ㄨ揪寮忓€间笉鍙兘涓虹┖鏃讹紝瀹為檯涓婂氨鏄湪缁熻琛屾暟銆傛渶绠€鍗曠殑灏辨槸褰撴垜浠娇鐢–OUNT()鐨勬椂鍊欙紝杩欑鎯呭喌涓嬮€氶厤绗?/em>骞朵笉鍍忔垜浠寽鎯崇殑閭f牱鎵╁睍鎴愭墍鏈夌殑鍒楋紝瀹為檯涓婏紝浠栦細蹇界暐鎵€鏈夊垪鑰岀洿鎺ョ粺璁℃墍鏈夌殑琛屾暟鈥溾€斺€斻€婇珮鎬ц兘MySQL銆嬨€?/p>

閫氬父锛屾垜浠皢绗竴涓瓧娈碉紙涓€鑸槸ID锛変綔涓轰富閿紝閭d箞杩欎釜鏃跺€機OUNT(id)瀹為檯缁熻鐨勫氨鏄鏁帮紝鍥犱负涓婚敭鑲畾鏄潪NULL鐨勩€傞棶棰樻槸Innodb鏄€氳繃涓婚敭绱㈠紩鏉ョ粺璁¤鏁扮殑鍚楋紵缁撹鏄細濡傛灉璇ヨ〃鍙湁涓€涓富閿储寮曪紝娌℃湁浠讳綍浜岀骇绱㈠紩鐨勬儏鍐典笅锛岄偅涔圕OUNT(*)鍜孋OUNT(1)閮芥槸閫氳繃閫氳繃涓婚敭绱㈠紩鏉ョ粺璁¤鏁扮殑銆傚鏋滆琛ㄦ湁浜岀骇绱㈠紩锛屽垯COUNT(1)鍜孋OUNT(*)閮戒細閫氳繃鍗犵敤绌洪棿鏈€灏忕殑瀛楁鐨勪簩绾х储寮?/strong>杩涜缁熻锛屼篃灏辨槸璇磋櫧鐒?/em>COUNT(id)鎸囧畾浜嗕富閿垪,浣嗘槸innodb涓嶄細鐪熺殑鍘荤粺璁′富閿储寮曘€?/p>

瀹為獙

CREATE TABLE `tb_news` ( `id` bigint(21) NOT NULL AUTO_INCREMENT, `title` varchar(50) NOT NULL, `content` mediumtext NOT NULL, `count_ass` char(1) NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=50001 DEFAULT CHARSET=utf8

杩欎釜鏃跺€欐墽琛孋OUNT(1)鍜孋OUNT(*)鍙互鐪嬪埌瑙i噴鍣ㄧ殑缁撴灉濡備笅锛堜袱鑰呬竴鑷达紝鎵€浠ュ氨鍙埅浜嗕竴寮犲浘锛夛紝鍙互鐪嬪埌锛屼袱鑰呴兘鐢ㄤ簡涓婚敭绱㈠紩杩涜琛屾暟鐨勭粺璁★細

鎶€鏈浘鐗? src=

鍘熺悊

鐩墠鍩轰簬纾佺洏鐨勬暟鎹簱鎴栬€呮悳绱㈠紩鎿庯紙姣斿Lucene锛夌殑鎬ц兘鐡堕涓昏閮芥槸鍦↖O闃舵锛岀浉姣斾簬CPU鍜孯AM锛孖O鎿嶄綔瀹炲湪澶參浜嗭紝鎵€浠ヨ繖绫荤郴缁熺殑浼樺寲鏂瑰悜涔熼兘閮芥槸绫讳技鐨勨€斺€斿敖涓€鍒囧彲鑳藉噺灏慖O鐨勬鏁帮紙鎵€浠ュ緢澶氱敤ES鐨勭▼搴忓湪鎬ц兘浼樺寲鍒版瀬闄愮殑鏃跺€欓€夋嫨鐩存帴涓奡SD锛夈€傝繖閲岀粺璁¤鏁扮殑鎿嶄綔锛屾煡璇紭鍖栧櫒鐨勪紭鍖栨柟鍚戝氨鏄€夋嫨鑳藉璁㊣O娆℃暟鏈€灏戠殑绱㈠紩锛屼篃灏辨槸鍩轰簬鍗犵敤绌洪棿鏈€灏忕殑瀛楁鎵€寤虹殑绱㈠紩锛堟瘡娆O璇诲彇鐨勬暟鎹噺鏄浐瀹氱殑锛岀储寮曞崰鐢ㄧ殑绌洪棿瓒婂皬鎵€闇€鐨処O娆℃暟涔熷氨瓒婂皯锛夈€傝€孖nnodb鐨勪富閿储寮曟槸鑱氱皣绱㈠紩锛堝寘鍚簡KEY锛岄櫎浜咾EY涔嬪鐨勫叾浠栧瓧娈靛€硷紝浜嬪姟ID鍜孧VCC鍥炴粴鎸囬拡锛夋墍浠ヤ富閿储寮曚竴瀹氫細姣斾簩绾х储寮曪紙鍖呭惈KEY鍜屽搴旂殑涓婚敭ID锛夊ぇ锛屼篃灏辨槸璇村湪鏈変簩绾х储寮曠殑鎯呭喌涓嬶紝涓€鑸珻OUNT()閮戒笉浼氶€氳繃涓婚敭绱㈠紩鏉ョ粺璁¤鏁帮紝鍦ㄦ湁澶氫釜浜岀骇绱㈠紩鐨勬儏鍐典笅閫夋嫨鍗犵敤绌洪棿鏈€灏忕殑銆?/p>

濡傛灉璇存湁寮營nnodb鐨勮〃鍙湁涓婚敭绱㈠紩锛岃€屼笖璁板綍杩樻瘮杈冨ぇ锛堟瘮濡?0K锛夛紝鍒欑粺璁¤鐨勬搷浣滀細闈炲父鎱紝鍥犱负IO娆℃暟浼氬緢澶氾紙杩欓噷灏变笉鍋氬疄楠屾埅鍥句簡锛屾湁鍏磋叮鍙互鑷繁璇曚竴涓嬶級銆?/p>

涓€涓紭鍖栨柟妗堝氨鏄鍏堝缓涓€涓皬瀛楁骞跺缓浜岀骇绱㈠紩涓撻棬鐢ㄦ潵缁熻琛屾暟锛屾瀬绔儏鍐典笅杩欑浼樺寲閫熷害鎻愰珮涓婂崈鍊嶄篃鏄甯哥殑銆?/p>

缁撹

缁撹灏辨槸瀵逛簬COUNT(1)鍜孋OUNT(*)鎵ц浼樺寲鍣ㄧ殑浼樺寲鏄畬鍏ㄤ竴鏍风殑锛屽苟娌℃湁COUNT(1)浼氭瘮COUNT(*)蹇繖涓娉曘€?/p>

鍗砪ount(瀛楁)<count(涓婚敭 id)<count(1)鈮坈ount(*)

以上是关于楂樻€ц兘MySQL鈥斺€攃ount(*) 鍜?count鍜宑ount(鍒楀悕)鍖哄埆的主要内容,如果未能解决你的问题,请参考以下文章

寮傚父鈥斺€攃md涓媕avac閿欒锛氱紪鐮丟BK涓嶅彲鏄犲皠瀛楃

缁忓吀鎺掑簭绠楁硶鈥斺€旈€夋嫨鎺掑簭

浣跨敤Recoding Rules浼樺寲鎬ц兘

Spring Cloud Feign鍘熺悊鍙婃€ц兘

鎬ц兘娴嬭瘯涔嬫柊涓€浠f湇鍔″櫒鎬ц兘娴嬭瘯宸ュ叿Gatling

mongodb鍙互閫氳繃profile鏉ョ洃鎺ф暟鎹?(mongodb鎬ц兘浼樺寲)

(c)2006-2024 SYSTEM All Rights Reserved IT常识