mysq>explain select count(*) from imageinfo;+----+-------------+-----------+-------+---------------+-------+---------+------ +---------+-------------+ | id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra | +----+-------------+-----------+-------+---------------+-------+---------+------ +---------+-------------+ | 1 | SIMPLE | imageinfo | index | NULL | width | 5 | NULL | 2051604 | Using index | +----+-------------+-----------+-------+---------------+-------+---------+------ +---------+-------------+从分析的结果来看,count(*)已经是采用了索引进行统计. 然后还发现了,这个工具的rows列输出为2051604,与实际记录数不符,为(2250440)
我用的是安装版的mysql5.0.18 for windows,MySQL Server Instance Configuration Wizard时候里有一个选项,决定mysql所使用的内存.选择一个种服务器:Developer Machine,Server Machine和Dedicated MySQL Server Machine三种所使用的内存分别是:最小内存,中等内存和最大的可供使用的内存。
+---------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref
| rows | Extra |
+----+-------------+-----------+-------+---------------+-------+---------+------
+---------+-------------+
| 1 | SIMPLE | imageinfo | index | NULL | width | 5 | NULL
| 2051604 | Using index |
+----+-------------+-----------+-------+---------------+-------+---------+------
+---------+-------------+从分析的结果来看,count(*)已经是采用了索引进行统计.
然后还发现了,这个工具的rows列输出为2051604,与实际记录数不符,为(2250440)
Server Machine
改成
select count(id) from userinfo
在大数据量的情况下,mysql的count(*)/count(id)是与IO的速度有关的,如果查询出来的记录数是100W条,那个mysql就得循环100W次来计算总数,所以速度就慢了.
关于limit,也是一样的道理,所以在limit 100W,XX的时候,也是循环100W次来查找记录,所以就有了这样的结果:
即使查询的结果集为0条记录,但用了limit 100W,10 也是很慢的...
至于count(*) 与count(id),count(id)确实能提高一点效率,但效果不理想.
由于以上原因,本人决定不用mysql进行查询,使用lucene进行配合查询...