Are conditional subqueries optimized out, if the condition is false?

Posted by Tobias Schulte on Stack Overflow See other posts from Stack Overflow or by Tobias Schulte
Published on 2010-05-20T14:48:25Z Indexed on 2010/05/20 15:10 UTC
Read the original article Hit count: 138

Filed under:
|
|
|
|

I have a table foo and a table bar, where each foo might have a bar (and a bar might belong to multiple foos).

Now I need to select all foos with a bar. My sql looks like this

SELECT * FROM foo f WHERE [...] AND ($param IS NULL OR 
    (SELECT ((COUNT(*))>0) FROM bar b WHERE f.bar = b.id))

with $param being replaced at runtime.

The question is: Will the subquery be executed even if param is null, or will the dbms optimize the subquery out?

We are using mysql, mssql and oracle. Is there a difference between these regarding the above?

© Stack Overflow or respective owner

Related posts about sql

Related posts about mysql