表product建立的索引:id、cate_id、cate_id2
select SQL_NO_CACHE id FROM product WHERE (cate_id=866 OR cate_id2=866) AND yn=1 AND online>=1 AND modified_time>'2011-01-01 00:00:00' LIMIT 93009,20;20 rows in set (0.68 sec)
select SQL_NO_CACHE id FROM product_base_relative WHERE (cate_id=866 OR cate_id2=866) AND yn=1 AND online>=1 AND modified_time>'2011-01-01 00:00:00' ORDER BY id LIMIT 93009,20;
20 rows in set (0.84 sec)
虽然用到了索引cate_id、cate_id2,但是如果对获取的id结果集进行排序,会慢一些,但不明显
select SQL_NO_CACHE * FROM product_base_relative WHERE (cate_id=866 OR cate_id2=866) AND yn=1 AND online>=1 AND modified_time>'2011-01-01 00:00:00' LIMIT 93009,20;
20 rows in set (0.67 sec)
select SQL_NO_CACHE * FROM product_base_relative WHERE (cate_id=866 OR cate_id2=866) AND yn=1 AND online>=1 AND modified_time>'2011-01-01 00:00:00' ORDER BY id LIMIT 93009,20;
20 rows in set (1.22 sec)
这里是取整个*,而不是id,慢接近一倍的时间,说明获取的列比较多,导致数据大小激增,排序速度减慢
同样
对比不加排序时获取*和id,时间差不多
对比加入排序是获取*和id,获取*时的查询也要慢不少,但不到一倍
(注意,前提是product表中包含较长的varchar字段)
其他资料:
http://www.mysqlperformanceblog.com/2006/09/01/order-by-limit-performance-optimization/
http://sumsung753.blog.163.com/blog/static/146364501201132410384888/
阅读(1667) | 评论(0) | 转发(0) |