麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 數(shù)據(jù)庫(kù) > MySQL > 正文

MySQL中count(*)、count(1)和count(col)的區(qū)別匯總

2024-07-25 19:08:09
字體:
供稿:網(wǎng)友

前言

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、插入測(cè)試數(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)

測(cè)試:

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é)

以上就是這篇文章的全部?jī)?nèi)容了,希望本文的內(nèi)容對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,如果有疑問大家可以留言交流,謝謝大家對(duì)VeVb武林網(wǎng)的支持。


注:相關(guān)教程知識(shí)閱讀請(qǐng)移步到MYSQL教程頻道。
發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 久久久久久亚洲国产精品 | 国产精品久久久久久久久久大牛 | 久久久久久久久国产精品 | 精品xxxx户外露出视频 | 蜜桃网站在线观看 | 欧美黄色一级生活片 | 成人在线视频在线观看 | 在线播放h | 少妇一级淫片免费放播放 | 九色新网址 | 蜜桃精品视频 | 欧美大电影免费观看 | 一区二区三区欧美在线观看 | 成人wxx视频免费 | 悠悠成人资源亚洲一区二区 | 午夜精品毛片 | 全黄裸片武则天一级第4季 九色p | 国产成年人在线观看 | 久久国产精品久久久久久 | 青草视频在线观看视频 | 亚洲精品一区二区三区在线看 | 中文字幕精品在线观看 | 欧美日韩国产中文字幕 | 叶子楣成人爽a毛片免费啪啪 | 亚洲午夜电影 | 在线免费黄色网 | 免费黄色小视频网站 | 黄色片网站在线免费观看 | 国产精品久久久久国产精品三级 | 欧美成人免费 | 在线a毛片免费视频观看 | 性aaa| 欧美成人视 | 欧美日本国产精品 | 色柚视频网站ww色 | 国产99精品视频 | 国产精品一区二区三区在线播放 | 国产精品亚洲综合一区二区三区 | 亚洲精品成人久久久 | 成年人在线免费播放视频 | 中文字幕伦乱 |