At first I want compare count(*) and count(id), which has better performance?
mysql version
5.6.21-1~dotdeb.1-log
table info
PRIMARY KEY (`id`),
KEY `is_availability` (`is_availability`,`is_del`)
ENGINE=InnoDB AUTO_INCREMENT=48993819 DEFAULT CHARSET=utf8
compare without where condition
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(*)
better than count(id)
compare with where condition
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(*)
still better thancount(id)
So if I could draw the conclusion that count(*)
has better performance than count(id)
and why is this?
From High Performance MySQL
, I got
if mysql knows some col cannot be NULL, it will optimize count(col) to count(*) internally
So I suspect that the time spend more is used to do this optimization work.