select count 和 select count(*)的区别

Posted jack kwok

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了select count 和 select count(*)的区别相关的知识,希望对你有一定的参考价值。

统计一个表T有多少行数据,通常写法是:

查询A:select count(*) from T

但也可以采用下面语句来查:

查询B:select count(1) from T

结果通常是一样的。那么二者区别在哪里呢?

如果T表是个很大的表,那么查询速度将有显著的差异。实践中T表有4200万行,采用查询B,耗时3分多钟,而采用查询A,则耗时不到1秒。可见在大表的查询上必须非常谨慎。

 

那么为什么查询A比查询B快呢?

个人分析认为,查询A不需要过滤,直接用末行位置-首行位置/每行占用位置。而查询B,因为每一行要与常数1进行比对,不是NULL才统计,所以就慢了。

以上是关于select count 和 select count(*)的区别的主要内容,如果未能解决你的问题,请参考以下文章

select * 和select 1,select count(*)和select count的区别

select count()和select count的区别

Select Count (*)和Select Count

select count和 select count(*)

实习MySQL-select count(*)和select count

[真伪]数据库中 select count(1) 比 select count(*) 快?