首页 > 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


相关文章 更多文章
详解MySQL的操作日志
MySQL常用存储引擎MyIsam和InnoDB的比较
书写Sql文件时常用的注释
认识MySQL的双向加密方式,处理显示乱码
MySQL数据库文件的备份和恢复方式
发表评论 0
网友评论
© 2010-2015 PekingPiao.com
版权所有 DBR
Mem
Top