views:

39

answers:

1

Take the following query:

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

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'
)
order by b

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

Which is best?

Edit Sorry I neglected to add the order by clause in the queries to indicate why it all needed to be a subquery in the first place!

Edit Ok, I thought it was going to be a simple answer, but I forgot I was talking about databases here! Running it through the analyser indicates that there is no optimization going on, but this could of course be because I only have 4 rows in my tables. I was hoping for a simpler yes or no.. Will have to set up some more complex tests.

I am on mySQL at the moment, but I am after a general answer here...

+2  A: 

Use:

SELECT x.a, 
       x.b
  FROM TABLE_X x
 WHERE x.a = 'mung'
UNION
SELECT y.a, 
       y.b
  FROM TABLE_Y y
 WHERE y.a = 'mung'
  • The subquery is unnecessary in the given context
  • The query above will use indexes if available on column "a"; Using the subquery method, there's no index to utilize
  • Use UNION ALL if you don't have to be concerned with duplicates - it will be faster than UNION
OMG Ponies
Apologies, I should have put the order by in the queries to indicate why a subquery is necessary in the first place. My silliness.. Still your other two points indicate that the optimizer will not be able to work out that the filter should be applied to the subqueries?
Mongus Pong
@Mongus Pong: Viewing the explain plan would be best to determine what the optimizer is doing, and it's possible it will be different between database vendors (if not versions too)
OMG Ponies