开发者

Does the optimizer filter subqueries with outer where clauses

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 开发者_JAVA百科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...


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜