MySql select from subquery slow (even though subquery on it's own is fast) -


I am trying to speed up this query:

  SELECT order.id FROM (`order` with 'o' select o.id oi.order_id = o.id and` o`.` canceled '= 0 and (`o`.` return_date`>" 2011-03 - 14 "OR oi.checked = 0 or '2011-03-14' & lt; oi.last_update)) As 'order' it clocks in 0.0930 seconds. Its subquery (SELECT * FROM  order  as  o  ...) on its clocks in 0.0005 seconds, the table I am testing contains approximately 10000 rows, 43 rows Returned by sub-clauses, where-clause  

In addition to accelerating the query, I would really love if someone could explain to me why when I wrap it in another query, the query becomes 100 times slower?

The MySQL interpretation tells me that it already selects o and then oi . MySQL first explains the full query (what is 2 derived on it?), Then o and oi in that order.

I am adding a lot because of this subquery being a subquisite which is not in the segment (which I have excluded from the code and benchmark) if I use the headquarters on it , Then with the Joint, the query will also slow down.

Any help would be greatly appreciated. I have searched to find the answers, but they can not be found, and this can be just because I do not know what to see, if so, then I apologize.

Post text "itemprop =" text ">

MySQL buffers the inline view before doing any of its work.

< Code> 0.0006 The second time you see the most response time (the first line is received) for the first query, the whole time (not the last row received).

If the ID is a primary command key on the table , you do not need group BY on it. < P> If your There are lots of items in the order (approximately 20 average or more according to the objects), you can try to make the following indexes:

  order_item (order_id, Checked) order_item (order_id, last_update))   

and your correlated subquery split into two: order from SELECT * where o.canceled = 0 And (o.return_date> 2011 -03-14 'or (select_yeme) from Er_item oi WHERE order_id = o.id) = 0 or (select MAX (last_update) from order_item oi WHERE oi.order_id = o .id) & gt; '2011-03-14')

Again, this will only help if you have many orders per item, otherwise a scan on all orders will be more efficient.

Comments