mysql count 解析

1.select count(1) from user   

2.select count(*) from user 

3.select count(name) from user 

4.select count(id) from user

id是主键 name是普通索引   哪种SQL语句查询的比较快

答:count(1), count(id), count(*)都是一样的,count(name)看name是不是表中最小的索引,如果不是最小的那会慢一点

count(*)相当于拿常量0 做统计 , count(1) 是拿常量1 做统计 ,除count(name)外,其余都是取最小的二级索引

count执行的时候是需要到存储引擎 读到数据记录,并执行mvcc找到可见版本,并给server层返回记录内容,最后server层去做统计处理。

原文地址:https://www.cnblogs.com/chengyunblogs/p/15411522.html