mysql下count(*),count(1)與count(column)區別

mysql下count(*),count(1)與count(column)區別mysql



count(*)對行的數目進行計算,包含NULLsql

count(column)對特定的列的值具備的行數進行計算,不包含NULL值。ide

count()還有一種使用方式,count(1)這個用法和count(*)的結果是同樣的。性能



關於他們的性能問題spa


1.任何狀況下SELECT COUNT(*) FROM tablename是最優選擇;索引

2.儘可能減小SELECT COUNT(*) FROM tablename WHERE COL = ‘value’ 這種查詢;it

3.杜絕SELECT COUNT(COL) FROM tablename WHERE COL2 = ‘value’ 的出現。innodb


若是表沒有主鍵,那麼count(1)比count(*)快。table

若是有主鍵,那麼count(主鍵,聯合主鍵)比count(*)快。class

若是表只有一個字段,count(*)最快。


1.myisam保存表的總行數,所以count(*)而且無where子句,很快會返回表的總行數

2.myisam保存表的總行數,利用count(column)而且無where子句,而且此column不爲null,很快會返回表的總行數

3.myisam保存表的總行數,利用count(column)而且無where子句,而且此column能夠爲null,mysql會對錶進行全表或全索引掃描來肯定行數

4.innodb查詢count(*),count(column(not null)),count(column(may be null))而且無where子句,mysql會對錶進行全表或全索引掃描來肯定行數

5.myisam和innodb查詢count(*),count(column(not null)),count(column(may be null))而且存在where子句,mysql會對錶進行索引掃描(若是列上有索引),速度也比較快

相關文章
相關標籤/搜索