起初我想要比较count(*)和count(id),它有更好的性能?
5.6.21-1~dotdeb.1-log
表信息
PRIMARY KEY (`id`),
KEY `is_availability` (`is_availability`,`is_del`)
ENGINE=InnoDB AUTO_INCREMENT=48993819 DEFAULT CHARSET=utf8
>比较没有条件
select count(*) from op_log;
+----------+
| count(*) |
+----------+
| 48989975 |
+----------+
1 row in set (10.02 sec)
select count(id) from op_log ;
+-----------+
| count(id) |
+-----------+
| 48989990 |
+-----------+
1 row in set (12.05 sec)
count(*)优于count(id)
>与条件比较
select count(*) from op_log where is_availability=1;
+----------+
| count(*) |
+----------+
| 48990038 |
+----------+
1 row in set (15.86 sec)
select count(id) from op_log where is_availability=1;
+-----------+
| count(id) |
+-----------+
| 48990096 |
+-----------+
1 row in set (17.13 sec)
count(*)仍然比count(*)更好
所以,如果我能得出结论count(*)具有比count(id)更好的性能,为什么会这样?
从高性能MySQL,我得到了
if mysql knows some col cannot be NULL, it will optimize count(col) to count(*) internally
所以我怀疑花费更多的时间用于做这项优化工作.