佳木斯湛栽影视文化发展公司

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

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

熱門標簽:地方門戶網(wǎng)站 百度競價排名 網(wǎng)站排名優(yōu)化 AI電銷 鐵路電話系統(tǒng) Linux服務器 服務外包 呼叫中心市場需求

前言

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

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、插入測試數(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呢,什么是二級索引?見下節(jié)

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

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

Extra有時候會顯示“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對于“Select tables optimized away”的含義, 不是"沒有更好的可優(yōu)化的了", 官方解釋中關鍵的地方在于:
 MySQL can read the result directly

所以,合理的解釋是: 

    1 數(shù)據(jù)已經(jīng)在內存中可以直接讀取; 

    2 數(shù)據(jù)可以被認為是一個經(jīng)計算后的結果,如函數(shù)或表達式的值; 

    3 一旦查詢的結果被優(yōu)化器"預判"可以不經(jīng)執(zhí)行就可以得到結果,所以才有"not need to perform the select".

總結

以上就是這篇文章的全部內容了,希望本文的內容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

您可能感興趣的文章:
  • MySQL 大表的count()優(yōu)化實現(xiàn)
  • MySQL中聚合函數(shù)count的使用和性能優(yōu)化技巧
  • 關于mysql中innodb的count優(yōu)化問題分享
  • 聊聊MySQL的COUNT(*)的性能
  • 詳解 MySQL中count函數(shù)的正確使用方法
  • 淺談MySQL 統(tǒng)計行數(shù)的 count
  • mysql count提高方法總結
  • MySQL中無過濾條件的count詳解
  • mySQL count多個表的數(shù)據(jù)實例詳解
  • MySQL COUNT函數(shù)的使用與優(yōu)化

標簽:仙桃 銅川 衡水 崇左 湖南 湘潭 蘭州 黃山

巨人網(wǎng)絡通訊聲明:本文標題《MySQL中count(*)、count(1)和count(col)的區(qū)別匯總》,本文關鍵詞  ;如發(fā)現(xiàn)本文內容存在版權問題,煩請?zhí)峁┫嚓P信息告之我們,我們將及時溝通與處理。本站內容系統(tǒng)采集于網(wǎng)絡,涉及言論、版權與本站無關。
  • 相關文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266
    兰州市| 河东区| 古浪县| 苗栗县| 时尚| 八宿县| 玛多县| 岑巩县| 永嘉县| 房产| 南乐县| 景谷| 武胜县| 大同市| 大足县| 古交市| 宾阳县| 万荣县| 营山县| 五原县| 临安市| 泸西县| 沙湾县| 普安县| 拉孜县| 清涧县| 齐齐哈尔市| 清丰县| 新沂市| 平泉县| 肇州县| 长兴县| 夏河县| 永平县| 阳新县| 宝清县| 措勤县| 罗城| 政和县| 扶绥县| 奎屯市|