婷婷综合国产,91蜜桃婷婷狠狠久久综合9色 ,九九九九九精品,国产综合av

主頁 > 知識庫 > MySQL中count(*)、count(1)和count(col)的區別匯總

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

熱門標簽:哈爾濱crm外呼系統價格 上海智能外呼系統需要多少錢 西安400電話在哪里辦理 中科嘉智人工智能電銷機器人 做地圖標注都需要什么工具 電銷機器人好品牌門薩維l 銀川電銷外呼系統定制 甘孜電話機器人廠家 凱立德科技館地圖標注

前言

count函數是用來統計表中或數組中記錄的一個函數,count(*) 它返回檢索行的數目, 不論其是否包含 NULL值。最近感覺大家都在討論count的區別,那么我也寫下吧:歡迎留言討論,話不多說了,來一起看看詳細的介紹吧。

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()優化實現
  • MySQL中聚合函數count的使用和性能優化技巧
  • 關于mysql中innodb的count優化問題分享
  • 聊聊MySQL的COUNT(*)的性能
  • 詳解 MySQL中count函數的正確使用方法
  • 淺談MySQL 統計行數的 count
  • mysql count提高方法總結
  • MySQL中無過濾條件的count詳解
  • mySQL count多個表的數據實例詳解
  • MySQL COUNT函數的使用與優化

標簽:平頂山 四川 浙江 安徽 那曲 安康 濮陽 山南

巨人網絡通訊聲明:本文標題《MySQL中count(*)、count(1)和count(col)的區別匯總》,本文關鍵詞  MySQL,中,count,和,col,的,區別,;如發現本文內容存在版權問題,煩請提供相關信息告之我們,我們將及時溝通與處理。本站內容系統采集于網絡,涉及言論、版權與本站無關。
  • 相關文章
  • 下面列出與本文章《MySQL中count(*)、count(1)和count(col)的區別匯總》相關的同類信息!
  • 本頁收集關于MySQL中count(*)、count(1)和count(col)的區別匯總的相關信息資訊供網民參考!
  • 推薦文章
    主站蜘蛛池模板: 广安市| 五大连池市| 和平县| 深水埗区| 塔城市| 萍乡市| 古田县| 济宁市| 铜陵市| 静安区| 花莲市| 台山市| 霍林郭勒市| 舒兰市| 济宁市| 乐至县| 祥云县| 香港| 乌兰县| 靖宇县| 新乐市| 宝山区| 射阳县| 平果县| 辽宁省| 石首市| 兴仁县| 穆棱市| 嘉祥县| 通州区| 佳木斯市| 合江县| 霍林郭勒市| 五台县| 游戏| 义乌市| 泗阳县| 营山县| 定边县| 龙门县| 巧家县|