我试图以良好的响应时间执行以下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
答案 0 :(得分:1)
如果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
答案 1 :(得分:0)
Sean提供的查询非常简单......那就是说,我会确保你在所有各自的表上都有以下索引
prod_orders ( id_order )
clients ( id_client )
clients_address ( id_client )
clienti_firme (id_client)
and of course your
orders ( id_order )
自从你提到MySQL以来我唯一的补充一点。尝试添加关键字“STRAIGHT_JOIN”...我不认为它会有所作为,因为你的所有表都是LEFT-JOIN,但你永远不知道。
SELECT STRAIGHT_JOIN ... rest of query ...
我能想到的另一件事可能是杀死它是某种笛卡尔数据结果。是的,订单应与单个客户关联,但如果该客户有多个地址(家庭,工作,备用办公室等,我们不知道您的表关系),再次为clienti_firme(客户关联的多个记录) (),你可能比你期望的更多。
如果您有一个客户的100个订单,并且该客户有3个地址并且与10个公司相关联,那么您现在有100 * 3 * 10个记录,您原本预计只有100个(现在获得3000个) 。对所有订单,客户等执行此操作,您就会发现它可能会成为一个问题。