1

This two SQL statements return equal results but first one is much slower than the second:

SELECT leading.email, kstatus.name, contacts.status 
FROM clients 
JOIN clients_leading ON clients.id_client = clients_leading.id_client 
JOIN leading ON clients_leading.id_leading = leading.id_leading 
JOIN contacts ON contacts.id_k_p = clients_leading.id_group 
JOIN kstatus on contacts.status = kstatus.id_kstatus 
WHERE (clients.email = 'some_email' OR clients.email1 = 'some_email') 
ORDER BY contacts.date DESC;



SELECT leading.email, kstatus.name, contacts.status
FROM (
     SELECT * 
     FROM clients 
     WHERE (clients.email = 'some_email' OR clients.email1 = 'some_email')
     ) 
AS clients  
JOIN clients_leading ON clients.id_client = clients_leading.id_client 
JOIN leading ON clients_leading.id_leading = leading.id_leading  
JOIN contacts ON contacts.id_k_p = clients_leading.id_group  
JOIN kstatus on contacts.status = kstatus.id_kstatus 
ORDER BY contacts.date DESC;

But I'm wondering why is it so? It looks like in the firt statement joins are done first and then WHERE clause is applied and in second is just the opposite. But will it behave the same way on all DB engines (I tested it on MySQL)?

I was expecting DB engine can optimize queries like the fors one and firs apply WHERE clause and then make joins.

4
  • 2
    You might be able to get a better answer to this question on dba.stackexchange.com Commented Jun 6, 2012 at 13:04
  • did you try this also under oracle? Commented Jun 6, 2012 at 13:33
  • unfortunately no, but I really wish to, because I'm pretty sure oracle would optimize it properly. Actually that's the purpose of my question - is this behaviour mysql-specific or valid on all engines. Commented Jun 6, 2012 at 13:36
  • it would be really good to have the explain plans of both queries Commented Aug 14, 2013 at 13:42

2 Answers 2

2

There are a lot of different reasons this could be (keying etc), but you can look at the explain mysql command to see how the statements are being executed. If you can run that and if it still is a mystery post it.

Sign up to request clarification or add additional context in comments.

Comments

0

You always can replace join with nested query... It's always faster but lot messy...

3 Comments

Your answer was flagged as low quality. Would you care to post references/ information to substantiate your suggestion? Without details or an example, such an uninformative answer would be deleted next time.
Ok, no problems I try to bring here my test and ressources as soon I have time... I know that my answer should be in comments when is short like that... :-(
Thanks for your input! References I Googled for & found seemed to lean the other way, but we value your participation & just generally like to see strong supporting details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.