简体   繁体   中英

SQL Slow Query Execution

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;

This query was supposedly executing slowly and was taking close to 10 minutes to execute how could query time be improved?

Check the execution plan. Look for table scans. Make sure that the relevant indexes exist on the tables. And change from the 'FROM X,Y' with the expensive WHERE clause syntax to an 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;

Try Below Query because o.cust_reference = 9 Also use in join so its execution is fast because join occur only on that rows which have 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;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM