1

I ‘m executing some sql statements on my data and I have the following situation.

Select statement looks like this

select *
from t1, t2
where t1.id = t2.t1_id
t1.c1 = 'X' and t1.c2  in ('Y', 'Z')

Running the explain plan I saw that the col2 filtering has cost 11 (Table Access with filter predicates) and the col1 1 (table access index).

How can I reduce the cost of the searching on col2? Should I put any hint in my query?

Thanks in advance,

2
  • 1
    Please paste the explain plan into the question. Commented Sep 1, 2013 at 13:50
  • ... And the real query. Commented Sep 1, 2013 at 18:29

1 Answer 1

1

I prefer the ANSI syntax for queries:

select *
from t1 join
     t2
     on t1.id = t2.t1_id
where t1.c1 = 'X' and t1.c2  in ('Y', 'Z');

Consider an index on t1(c1, c2, id) and t2(t1_id). The first index should be used to get the rows from t1. The second should speed up the join to t2.

EDIT:

As per David's comment, an index on t1(c1, c2) might perform slightly better.

Sign up to request clarification or add additional context in comments.

3 Comments

With the select * getting all columns from both tables, the index on t1 might as well be (c1, c2) I think.
@DavidAldridge . . . That is an interesting point. It is possible (but I do not know) that with the id in the index, the index alone would be used for the join with the additional columns coming later. However, Oracle might fetch the pages first and then do the join.
I'm pretty sure it would access the table first, but whichever way it does I think that the net work would be lower without the id on the index -- you'd also get a more compact index, and COMPRESS might be useful as well.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.