我们用到了两个数据量比较大的表,表table1和表table2,每个表的数据都是四五百万条。
table1三个字段:id(主键),age,name
table2两个字段:id(主键),status 执行下面的查询语句:
select t1.* from table1 as t1 inner join table2 as t2 on t1.id=t2.id where t1.age>=18 and t1.name like '%a%' order by t2.status desc,t1.id desc limit 10 查询耗时:25秒左右。
一开始只有name有索引,在排查问题过程中我们给table2的status加上了索引。但是查询耗时并没有减少。
后来发现是对两个表的两个字段进行order by导致的。
单独执行order by t2.status desc或者order by t1.id desc查询时间都在0.1秒之内。 不明何故,还望高手解释下。
table1三个字段:id(主键),age,name
table2两个字段:id(主键),status 执行下面的查询语句:
select t1.* from table1 as t1 inner join table2 as t2 on t1.id=t2.id where t1.age>=18 and t1.name like '%a%' order by t2.status desc,t1.id desc limit 10 查询耗时:25秒左右。
一开始只有name有索引,在排查问题过程中我们给table2的status加上了索引。但是查询耗时并没有减少。
后来发现是对两个表的两个字段进行order by导致的。
单独执行order by t2.status desc或者order by t1.id desc查询时间都在0.1秒之内。 不明何故,还望高手解释下。
http://topic.csdn.net/u/20090526/17/639d78ec-e299-40d0-9c8e-8d5b21229405.html?2724
http://topic.csdn.net/u/20090520/16/a96a2e90-a935-4460-837e-e52b4557c519.html?48098
如果想了解原理的话,则建议看一下《数据库系统概论》中的优化那一章节。