我可以改进这个mysql查询的速度吗?

时间:2012-12-11 19:02:36

标签: mysql sql database

我正在使用testlink进行内部存储系统测试。以下查询大约需要80秒,并返回大约11k行数据。是否有任何可以改进的东西?我假设它是两个左外连接是瓶颈。

当前速度:

(11,002 total, Query took 82.8888 sec)

以下是查询:

SELECT UA.build_id AS build_id, 
   UA.feature_id AS feature_id,
   UA.user_id as user_id,   
   TPTCV.testplan_id AS testplan_id,
   TPTCV.tcversion_id AS tcversion_id,
   TPTCV.platform_id AS platform_id,       
   E.status AS status,
   E.id as execution_id,
   E.tester_id as tester_id
FROM user_assignments UA  
LEFT OUTER JOIN testplan_tcversions TPTCV ON UA.feature_id = TPTCV.id  
LEFT OUTER JOIN executions E ON TPTCV.tcversion_id = E.tcversion_id
AND UA.build_id = E.build_id
AND TPTCV.platform_id = E.platform_id
WHERE UA.type = 1 AND UA.build_id = 10
ORDER BY E.id DESC

2 个答案:

答案 0 :(得分:3)

您是否在外键列上有索引?你需要它们。如果可能,在所有连接列上。还生成执行计划并查看输出,这对性能分析非常有用。如果您不理解,请在此处发布。

http://dev.mysql.com/doc/refman/5.1/en/execution-plan-information.html

您的ORDER BY子句也可能非常昂贵。躲开它。如果可能的话,在别处排序。

答案 1 :(得分:0)

考虑添加覆盖索引,因此可以完全从索引块中满足查询,而无需引用表块。

e.g。

... ON user_assignments (build_id, type, feature_id, user_id)

... ON testplan_tcversions (id, testplan_id, tcversion_id, platform_id)

... ON executions (tcversion_id, platform_id, build_id,  status, tester_id, id)

您也可以尝试在没有ORDER BY的情况下运行查询,以消除排序操作的需要。