How can I get Oracle to use indexes when my view contains a UNION ALL?
Question
VIEW1 IS:
SELECT A, B, C FROM view2
UNION ALL
SELECT A, B, C FROM view3
INNER JOIN TBL5 ON view3.CODE = TBL5.CODE
Column C is indexed in the source tables, and when I execute either of the select statements individually it uses the index and comes back in a flash. When I use the view it times out. I was under the impression that Oracle rewrote queries against views and used indexes where useful rather than doing a SELECT * FROM VIEW1
and then applying the predicates after the fact.
What am I doing wrong? The sample views above illuminate the issue but my real views each join dozens of tables so a view is really necessary.
Solution
The optimizer does not know what the value of CODE will be ahead of time when it builds the execution plan because you are going against the view. If you are in 11g you can use the PUSH_PRED optimizer hint to make it work as you expect.
OTHER TIPS
you may try a HINT. or make sure the tables are analyzed.
one other thought - maybe make a Meterialized View?