简体   繁体   English

SQL查询的执行时间很长

[英]Very long execution time for sql query

I have trying to execute the following sql query with good response time, but when I add the ORDER BY statement the query doesn't return anything for more than 5 min. 我试图以良好的响应时间执行以下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 

If the Order By is slowing it down try: 如果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 

Maybe the execution plan of all of the derived tables is making it run SELECT * for each row of the first table (orders). 也许所有派生表的执行计划都使其对第一个表的每一行(订单)运行SELECT *。 Try this instead: 尝试以下方法:

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 

The query provided by Sean is about as simple as it would get... That said, I would ensure you have the following indexes on all respective tables Sean提供的查询将尽可能简单...也就是说,我将确保您在所有各自的表上均具有以下索引

prod_orders ( id_order )
clients ( id_client )
clients_address ( id_client )
clienti_firme (id_client)

and of course your 
orders ( id_order )

The only ADDITIONAL thing I would since you mentioned MySQL. 自从您提到MySQL之后,我唯一要做的事情就是。 try adding the keyword "STRAIGHT_JOIN"... I don't think it would make a difference since all your tables are LEFT-JOINs, but you never know. 尝试添加关键字“ STRAIGHT_JOIN” ...我认为这不会有所作为,因为您的所有表都是LEFT-JOIN,但是您永远不会知道。

SELECT STRAIGHT_JOIN ... rest of query ...

The only other thing I can think of that may be killing it is some sort of Cartesian result of data. 我唯一想到的可能是杀死它,这是某种笛卡尔数据的结果。 yes, an order should be associated with a single client, but if that client has multiple addresses (home, work, alternate office, etc and we don't know your table relationships), and again for clienti_firme (multiple records the client is associated with), you could be blowing through more than you expect. 是的,一个订单应该与一个客户相关联,但是如果该客户有多个地址(家庭,工作,替代办公室等,我们不知道您与表的关系),则再次针对clienti_firme(与该客户相关的多个记录) ),那么您的努力可能会超出预期。

If you have 100 orders for a single client, and that client has 3 addresses and is associated with 10 firms, you now have 100 * 3 * 10 records coming through where you may have originally expected only 100 (now getting 3000). 如果单个客户有100个订单,并且该客户有3个地址并且与10个公司相关联,则现在有100 * 3 * 10条记录通过,而您最初可能只期望100条记录(现在获得3000条记录)。 Do that with all the orders, clients, etc and you see how it COULD become a problem. 对所有订单,客户等执行此操作,您将看到它可能成为问题。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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