How can I get Oracle to have a better execution plan for this query?
We have some tables which keep track of processed transactions. These
tables have millions of rows. Often times I want to look at the most
recent X transactions, so I have a query like this to pull the info I want
from a few tables:
select a.id, b.field_one, c.field_two
from trans a, table_two b, table_three c
where a.id = b.id
and a.id = c.id
and a.id in
(select id from trans where id > (select max(id) from trans) - 100);
Right now the query is very slow. The explain plan shows a full table scan
on B and C. Now, if I evaluate the nested query separately and replace it
with a list of comma separated IDs, the query is very fast. This seems
obvious to me - it will only have 100 rows to join together so of course
it will be faster than if it answered the query by first joining A and B
together.
Conceptually I understand the query optimizer is trying to find a good
execution plan but in this case it seems like it is doing a terrible job.
Is there any way to force the DBMS to execute the nested query first?
Possibly by using a Hint?
Thanks
No comments:
Post a Comment