SQL慢查询执行

时间:2014-11-10 05:09:12

标签: sql query-performance

SELECT s.supplier_id, s.supplier_name, o.order_date
FROM suppliers s, orders o
WHERE s.supplier_id = o.supplier_id
AND o.cust_reference = 9
ORDER BY o.order_date;

这个查询据说执行缓慢,接近 10分钟来执行如何改善查询时间?

2 个答案:

答案 0 :(得分:2)

检查执行计划。寻找表扫描。确保表中存在相关索引。并使用昂贵的WHERE子句语法从“FROM X,Y”更改为INNER JOIN

SELECT s.supplier_id, s.supplier_name, o.order_date
FROM suppliers s
INNER JOIN orders o ON s.supplier_id = o.supplier_id
WHERE o.cust_reference = 9
ORDER BY o.order_date;

答案 1 :(得分:1)

尝试以下查询,因为o.cust_reference = 9也可以在join中使用,因此它的执行速度很快,因为只有在cust_reference = 9的行上才会发生连接

SELECT s.supplier_id, s.supplier_name, o.order_date
FROM suppliers s
INNER JOIN orders o ON s.supplier_id = o.supplier_id
AND o.cust_reference = 9
ORDER BY o.order_date;