Does the optimizer filter subqueries with outer where clauses

Posted by Mongus Pong on Stack Overflow See other posts from Stack Overflow or by Mongus Pong
Published on 2010-03-27T01:13:04Z Indexed on 2010/03/27 1:23 UTC
Read the original article Hit count: 279

Filed under:
|
|

Take the following query:

select * from
(
  select a, b
  from c
    UNION
  select a, b
  from d
)
where a = 'mung'

Will the optimizer generally work out that I am filtering a on the value 'mung' and consequently filter mung on each of the queries in the subquery.

OR

will it run each query within the subquery union and return the results to the outer query for filtering (as the query would perhaps suggest)

In which case the following query would perform better :

select * from
(
  select a, b
  from c
  where a = 'mung'
    UNION
  select a, b
  from d
  where a = 'mung'
)

Obviously query 1 is best for maintenance, but is it sacrificing much performace for this?

Which is best?

© Stack Overflow or respective owner

Related posts about sql

Related posts about Performance