count(1)、count(*)與count(列名)的執(zhí)行區(qū)別

1.? count(1) and count(*)

當(dāng)表的數(shù)據(jù)量大些時(shí),對(duì)表作分析之后,使用count(1)還要比使用count(*)用時(shí)多了!?

從執(zhí)行計(jì)劃來看,count(1)和count(*)的效果是一樣的。 但是在表做過分析之后,count(1)會(huì)比count(*)的用時(shí)少些(1w以內(nèi)數(shù)據(jù)量),不過差不了多少。?


如果count(1)是聚索引,id,那肯定是count(1)快。但是差的很小的。?

因?yàn)閏ount(*),自動(dòng)會(huì)優(yōu)化指定到那一個(gè)字段。所以沒必要去count(1),用count(*),sql會(huì)幫你完成優(yōu)化的 因此:count(1)和count(*)基本沒有差別!?


2. count(1) and count(字段)

兩者的主要區(qū)別是

(1) count(1) 會(huì)統(tǒng)計(jì)表中的所有的記錄數(shù),包含字段為null 的記錄。

(2) count(字段) 會(huì)統(tǒng)計(jì)該字段在表中出現(xiàn)的次數(shù),忽略字段為null 的情況。即不統(tǒng)計(jì)字段為null 的記錄。?

轉(zhuǎn)自:http://www.cnblogs.com/Dhouse/p/6734837.html

count(*) 和 count(1)和count(列名)區(qū)別 ?

執(zhí)行效果上: ?

count(*)包括了所有的列,相當(dāng)于行數(shù),在統(tǒng)計(jì)結(jié)果的時(shí)候,不會(huì)忽略列值為NULL ?

count(1)包括了忽略所有列,用1代表代碼行,在統(tǒng)計(jì)結(jié)果的時(shí)候,不會(huì)忽略列值為NULL ?

count(列名)只包括列名那一列,在統(tǒng)計(jì)結(jié)果的時(shí)候,會(huì)忽略列值為空(這里的空不是只空字符串或者0,而是表示null)的計(jì)數(shù),即某個(gè)字段值為NULL時(shí),不統(tǒng)計(jì)。

執(zhí)行效率上: ?

列名為主鍵,count(列名)會(huì)比count(1)快 ?

列名不為主鍵,count(1)會(huì)比count(列名)快 ?

如果表多個(gè)列并且沒有主鍵,則 count(1) 的執(zhí)行效率優(yōu)于 count(*) ?

如果有主鍵,則 select count(主鍵)的執(zhí)行效率是最優(yōu)的 ?

如果表只有一個(gè)字段,則 select count(*)最優(yōu)。

轉(zhuǎn)自:http://eeeewwwqq.iteye.com/blog/1972576

實(shí)例分析

mysql> create table counttest(name char(1), age char(2));

Query OK, 0 rows affected (0.03 sec)

mysql> insert into counttest values

? ? -> ('a', '14'),('a', '15'), ('a', '15'),

? ? -> ('b', NULL), ('b', '16'),

? ? -> ('c', '17'),

? ? -> ('d', null),

? ? ->('e', '');

Query OK, 8 rows affected (0.01 sec)

Records: 8? Duplicates: 0? Warnings: 0

mysql> select * from counttest;

+------+------+

| name | age? |

+------+------+

| a? ? | 14? |

| a? ? | 15? |

| a? ? | 15? |

| b? ? | NULL |

| b? ? | 16? |

| c? ? | 17? |

| d? ? | NULL |

| e? ? |? ? ? |

+------+------+

8 rows in set (0.00 sec)

mysql> select name, count(name), count(1), count(*), count(age), count(distinct(age))

? ? -> from counttest

? ? -> group by name;

+------+-------------+----------+----------+------------+----------------------+

| name | count(name) | count(1) | count(*) | count(age) | count(distinct(age)) |

+------+-------------+----------+----------+------------+----------------------+

| a? ? |? ? ? ? ? 3 |? ? ? ? 3 |? ? ? ? 3 |? ? ? ? ? 3 |? ? ? ? ? ? ? ? ? ? 2 |

| b? ? |? ? ? ? ? 2 |? ? ? ? 2 |? ? ? ? 2 |? ? ? ? ? 1 |? ? ? ? ? ? ? ? ? ? 1 |

| c? ? |? ? ? ? ? 1 |? ? ? ? 1 |? ? ? ? 1 |? ? ? ? ? 1 |? ? ? ? ? ? ? ? ? ? 1 |

| d? ? |? ? ? ? ? 1 |? ? ? ? 1 |? ? ? ? 1 |? ? ? ? ? 0 |? ? ? ? ? ? ? ? ? ? 0 |

| e? ? |? ? ? ? ? 1 |? ? ? ? 1 |? ? ? ? 1 |? ? ? ? ? 1 |? ? ? ? ? ? ? ? ? ? 1 |

+------+-------------+----------+----------+------------+----------------------+

5 rows in set (0.00 sec)

額外參考資料:http://blog.csdn.net/lihuarongaini/article/details/68485838

---------------------

作者:BigoSprite

來源:CSDN

原文:https://blog.csdn.net/ifumi/article/details/77920767

版權(quán)聲明:本文為博主原創(chuàng)文章,轉(zhuǎn)載請(qǐng)附上博文鏈接!

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容