久久精品人人爽,华人av在线,亚洲性视频网站,欧美专区一二三

MySQL中count(*)、count(1)、count(col)三者的區別是什么

156次閱讀
沒有評論

共計 2589 個字符,預計需要花費 7 分鐘才能閱讀完成。

自動寫代碼機器人,免費開通

MySQL 中 count(*)、count(1)、count(col) 三者的區別是什么?針對這個問題,這篇文章詳細介紹了相對應的分析和解答,希望可以幫助更多想解決這個問題的小伙伴找到更簡單易行的方法。

1、表結構:

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、插入測試數據:

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 呢,什么是二級索引?見下節

count(*) 和 count(1) 是沒有區別的,而 count(col) 是有區別的

執行計劃有特點:可以看出它沒有查詢索引和表,有時候會出現 select tables optimized away 不會查表,速度會很快

Extra 有時候會顯示“Select tables optimized away”,意思是沒有更好的可優化的了。

官方解釋 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 對于“Select tables optimized away”的含義, 不是 沒有更好的可優化的了 , 官方解釋中關鍵的地方在于:
 MySQL can read the result directly

所以, 合理的解釋是: 

  1 數據已經在內存中可以直接讀取; 

  2 數據可以被認為是一個經計算后的結果, 如函數或表達式的值; 

  3 一旦查詢的結果被優化器 預判 可以不經執行就可以得到結果, 所以才有 not need to perform the select .

關于 MySQL 中 count(*)、count(1)、count(col) 三者的區別是什么問題的解答就分享到這里了,希望以上內容可以對大家有一定的幫助,如果你還有很多疑惑沒有解開,可以關注丸趣 TV 行業資訊頻道了解更多相關知識。

向 AI 問一下細節

正文完
 
丸趣
版權聲明:本站原創文章,由 丸趣 2023-12-04發表,共計2589字。
轉載說明:除特殊說明外本站除技術相關以外文章皆由網絡搜集發布,轉載請注明出處。
評論(沒有評論)
主站蜘蛛池模板: 沂源县| 老河口市| 易门县| 茂名市| 海林市| 高唐县| 偃师市| 景德镇市| 崇仁县| 海阳市| 上犹县| 秀山| 陇南市| 衡阳县| 南康市| 龙州县| 福清市| 香格里拉县| 巩义市| 怀远县| 都江堰市| 巴彦县| 双峰县| 沧州市| 张家港市| 大关县| 九龙县| 同心县| 新宾| 泊头市| 西城区| 鄂尔多斯市| 乐平市| 大足县| 邵阳市| 乌兰察布市| 庆阳市| 五指山市| 遂昌县| 紫阳县| 嵩明县|