subquery - MYSQL sub query performance inconsistency -


g-day,

i'm puzzled following. when run query takes long time resolve:

select * test id in (select id test2 customer = 'harry'); 

when run sub query resolves '13454' in spit second. when change main query's statement in ('13454') resolves instantly.

someone have idea why won't run in it's original format??

cheers,

rene

the reason quirk of mysql. subquery executed each row of outer table.

to fix this, use exists:

select * test exists (select id test2 test2.customer = 'harry' , test2.id = test.id); 

this work efficiently if have index on test2(customer, id).

i should add depends on version of mysql (i think fixed in 5.5). older documentation explains as:

consider following subquery comparison:

outer_expr in (select inner_expr ... subquery_where)

mysql evaluates queries “from outside inside.” is, first obtains value of outer expression outer_expr, , runs subquery , captures rows produces.


Comments

Popular posts from this blog

Why does Ruby on Rails generate add a blank line to the end of a file? -

keyboard - Smiles and long press feature in Android -

node.js - Bad Request - node js ajax post -