MySQL中count(*)、count(1)和count(col)的區(qū)別匯總
前言
count函數(shù)是用來統(tǒng)計(jì)表中或數(shù)組中記錄的一個(gè)函數(shù),count(*) 它返回檢索行的數(shù)目, 不論其是否包含 NULL值。最近感覺大家都在討論count的區(qū)別,那么我也寫下吧:歡迎留言討論,話不多說了,來一起看看詳細(xì)的介紹吧。
1、表結(jié)構(gòu):
dba_jingjing@3306>[rds_test]>CREATE TABLE `test_count` ( -> `c1` varchar(10) DEFAULT NULL, -> `c2` varchar(10) DEFAULT NULL, -> KEY `idx_c1` (`c1`) -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8; Query OK, 0 rows affected (0.11 sec)
2、插入測試數(shù)據(jù):
dba_jingjing@3306>[rds_test]>insert into test_count values(1,10); Query OK, 1 row affected (0.03 sec) dba_jingjing@3306>[rds_test]>insert into test_count values(abc,null); ERROR 1054 (42S22): Unknown column 'abc' in 'field list' dba_jingjing@3306>[rds_test]>insert into test_count values('abc',null); Query OK, 1 row affected (0.04 sec) dba_jingjing@3306>[rds_test]>insert into test_count values(null,null); Query OK, 1 row affected (0.04 sec) dba_jingjing@3306>[rds_test]>insert into test_count values('368rhf8fj',null); Query OK, 1 row affected (0.03 sec) dba_jingjing@3306>[rds_test]>select * from test_count; +-----------+------+ | c1 | c2 | +-----------+------+ | 1 | 10 | | abc | NULL | | NULL | NULL | | 368rhf8fj | NULL | +-----------+------+ 4 rows in set (0.00 sec)
測試:
dba_jingjing@3306>[rds_test]>select count(*) from test_count; +----------+ | count(*) | +----------+ | 4 | +----------+ 1 row in set (0.00 sec) EXPLAIN: { "query_block": { "select_id": 1, "message": "Select tables optimized away" 1 row in set, 1 warning (0.00 sec)
dba_jingjing@3306>[rds_test]>select count(1) from test_count; +----------+ | count(1) | +----------+ | 4 | +----------+ 1 row in set (0.00 sec) EXPLAIN: { "query_block": { "select_id": 1, "message": "Select tables optimized away" 1 row in set, 1 warning (0.00 sec)
dba_jingjing@3306>[rds_test]>select count(c1) from test_count; +-----------+ | count(c1) | +-----------+ | 3 | +-----------+ 1 row in set (0.00 sec) "table": { "table_name": "test1", "access_type": "index", "key": "idx_c1", "used_key_parts": [ "c1" ], "key_length": "33",
那么這里面的"key_length": "33",為什么是33呢,什么是二級(jí)索引?見下節(jié)
count(*) 和count(1) 是沒有區(qū)別的,而count(col) 是有區(qū)別的
執(zhí)行計(jì)劃有特點(diǎn):可以看出它沒有查詢索引和表,有時(shí)候會(huì)出現(xiàn)select tables optimized away 不會(huì)查表,速度會(huì)很快
Extra有時(shí)候會(huì)顯示“Select tables optimized away”,意思是沒有更好的可優(yōu)化的了。
官方解釋For explains on simple count queries (i.e. explain select count(*) from people) the extra
section will read "Select tables optimized away."
This is due to the fact that MySQL can read the result directly from the table internals and therefore does not need to perform the select.
---MySQL對(duì)于“Select tables optimized away”的含義, 不是"沒有更好的可優(yōu)化的了", 官方解釋中關(guān)鍵的地方在于:
MySQL can read the result directly
所以,合理的解釋是:
1 數(shù)據(jù)已經(jīng)在內(nèi)存中可以直接讀取;
2 數(shù)據(jù)可以被認(rèn)為是一個(gè)經(jīng)計(jì)算后的結(jié)果,如函數(shù)或表達(dá)式的值;
3 一旦查詢的結(jié)果被優(yōu)化器"預(yù)判"可以不經(jīng)執(zhí)行就可以得到結(jié)果,所以才有"not need to perform the select".
總結(jié)
以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)腳本之家的支持。
- MySQL count(1)、count(*)、count(字段)的區(qū)別
- Mysql中count(*)、count(1)、count(主鍵id)與count(字段)的區(qū)別
- MySQL?中的count(*)?與?count(1)?誰更快一些?
- MySQL?count(*),count(id),count(1),count(字段)區(qū)別
- 一文搞清楚MySQL count(*)、count(1)、count(col)區(qū)別
- MySQL中的count(*)?和?count(1)?區(qū)別性能對(duì)比分析
- SQL中count(1)、count(*)?與?count(列名)的區(qū)別詳細(xì)解釋
相關(guān)文章
MySQL 兩張表數(shù)據(jù)合并的實(shí)現(xiàn)
本文主要介紹了MySQL 兩張表數(shù)據(jù)合并的實(shí)現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2023-01-01深入理解MySQL數(shù)據(jù)類型的選擇優(yōu)化
這篇文章主要介紹了深入理解MySQL數(shù)據(jù)類型的選擇優(yōu)化,MySQL數(shù)據(jù)類型是定義列中可以存儲(chǔ)什么數(shù)據(jù)以及該數(shù)據(jù)實(shí)際怎樣存儲(chǔ)的基本規(guī)則,正確的選擇數(shù)據(jù)庫字段的字段類型對(duì)于數(shù)據(jù)庫性能有很大的影響2022-08-08內(nèi)網(wǎng)ssh/mysql登錄緩慢的解決方法
本文介紹了“內(nèi)網(wǎng)ssh/mysql登錄緩慢的解決方法”,需要的朋友可以參考一下2013-03-03MySQL刪除表時(shí)I/O錯(cuò)誤的原因分析與解決
這篇文章主要給大家介紹了關(guān)于MySQL刪除表時(shí)I/O錯(cuò)誤的原因分析與解決方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-08-08mysql滑動(dòng)聚合/年初至今聚合原理與用法實(shí)例分析
這篇文章主要介紹了mysql滑動(dòng)聚合原理與用法,結(jié)合實(shí)例形式分析了mysql滑動(dòng)聚合的相關(guān)功能、原理、使用方法及操作注意事項(xiàng),需要的朋友可以參考下2019-12-12MySQL事務(wù)的隔離性是如何實(shí)現(xiàn)的
最近做了一些分布式事務(wù)的項(xiàng)目,對(duì)事務(wù)的隔離性有了更深的認(rèn)識(shí),后續(xù)寫文章聊分布式事務(wù)。今天就復(fù)盤一下單機(jī)事務(wù)的隔離性是如何實(shí)現(xiàn)的?感興趣的可以了解一下-2021-09-09