2011-11-28 24 views
1

我有这样一个简单的查询:MySQL - 可以加快MySQL数量?

select count(*) from table 

我可以做加速MySQL的:

select count(id) from table 

这是否在速度方面有什么区别?

最好的问候,

+0

http://stackoverflow.com/questions/1031312/how-to-speed-up-select-count-with-group-by-and-where – LostMohican

+0

http://stackoverflow.com/questions/59294/in -sql-what-the-difference-between-countcolumn-and-count – esskar

回答

1

id是你的主键。 *将简单映射到您的主键。 count(id) = count(*)

1

你可以尝试从INFORMATION_SCHEMA

select TABLE_ROWS from information_schema.tables 
where TABLE_SCHEMA = '$db' and TABLE_NAME = '$tbl'; 
+0

MySql不会在count(*)的封面下使用这些表吗? –

+0

count可以结合filter,group by ...但是这个information_schema.tables是表中行的普通计数 – ajreal

0

得到计数如果id是你的主键,然后没有也不会,因为MySQL将使用主键被窝里呢。如果id不是主键,那么它实际上可能会使查询稍微慢一些。

0

如果id是“not null”,那么它不会有任何区别。

如果id是可以为空的,那么它是一个不同的查询并且不会(总是)产生相同的结果。