select count() , select count(1)和select count(keyId)优化如何选择

 

1、select count(*) from table

2、select count(1) from table

3、select count(keyId) from table

有主键(Primary key)

3>1>2


沒有主键(Primary key),

2>1

一般情况下,Select Count (*)和Select Count(1)两着返回结果是一样的。
如果你的表只有一个字段的话那count(*)就是最快的,count(*) 跟 count(1) 的结果一样,都包括对NULL的统计,而count(id) 是不包括NULL的统计。

当用某一具体字段作为count函数的参数时,值为null的记录是不被计算的。

 

posted @ 2020-07-29 08:51  Kaspar_Choo  阅读(455)  评论(0编辑  收藏  举报