首页 > MySQL
MySQl 查询 count(*) count(1) count(主键) 选哪个
发布时间:2017-06-14 12:03:53
访问量:6

    查询同一张表,不考虑是否有null ,以下是实测模拟百万级用户量多次执行查询,分别执行时间的区间:

select count(*) from usero;  0.516s-0.566s
select count(1) from usero; 0.507s - 0.569s
select count(id) from usero;主键  0.655s-0.707s
select count(name) from usero; 普通索引 0.701-0.747s
select count(phone) from usero; 非索引 1.041s-1.138s

ps:

1.count(*) mysql会转为 count(1), count(ID)只会计算 not null值; 

2.count(1)代表查询的表里的第一个字段。(有争议:另一种理论是主键,个人认为前一种);

3.使用explain 查看sql执行是否使用索引,count(*) count(1) count(id) count(name) => Using Index;


参考:https://www.zhihu.com/question/19641756


相关文章 更多文章
远程连接虚拟机Centos6.5中MySQL
sql 根据in条件排序查询结果
补充 MySQl 查询 count(*) count(1) count(主键) 怎样选择索引的
mysql基础再回顾
MySQL错误之‘Got a packet bigger than 'max_allowed_packet' bytes’
发表评论 0
网友评论
© 2010-2015 PekingPiao.com
版权所有 DBR
Mem
Top