我试图以良好的响应时间执行以下sql查询,但是当我添加ORDER BY语句时,查询在5分钟内未返回任何内容。

SELECT *
FROM orders co
LEFT JOIN (SELECT * FROM prod_orders) AS pc
ON co.id_order = pc.id_order
LEFT JOIN (SELECT * FROM clients) AS cl
ON co.id_client = cl.id_client
LEFT JOIN (SELECT * FROM clients_address) AS ca
ON co.id_client = ca.id_client
LEFT JOIN (SELECT * FROM clienti_firme) AS cf
ON ca.id_client = cf.id_client
ORDER BY co.id_order

最佳答案

如果Order By正在减慢速度,请尝试:

SELECT *
FROM (SELECT * FROM orders ORDER BY id_order) AS co
LEFT JOIN prod_orders AS pc
ON co.id_order = pc.id_order
LEFT JOIN clients AS cl
ON co.id_client = cl.id_client
LEFT JOIN clients_address AS ca
ON co.id_client = ca.id_client
LEFT JOIN clienti_firme AS cf
ON ca.id_client = cf.id_client
ORDER BY co.id_order


也许所有派生表的执行计划都使其对第一个表的每一行(订单)运行SELECT *。尝试以下方法:

SELECT *
FROM orders co
LEFT JOIN prod_orders AS pc
ON co.id_order = pc.id_order
LEFT JOIN clients AS cl
ON co.id_client = cl.id_client
LEFT JOIN clients_address AS ca
ON co.id_client = ca.id_client
LEFT JOIN clienti_firme AS cf
ON ca.id_client = cf.id_client
ORDER BY co.id_order

关于mysql - sql查询的执行时间很长,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21390522/

10-16 09:07