Reputation: 199
I have table
test_A(
id1 number,
id2 number,
id3 number,
name varchar2(10),
create_dt date
)
I have two indexes one composite index indx1
on (id1,id2)
and indx2(id3)
. Now when I query this table test_A
as
select * from test_A where id2=123 and
create_dt=(select max(create_dt) from test_A where test_A.id2=id2);
I ran explain plan for this above SQL and it is using "index skip scan". If I create another index on create_dt
then it using index fast full scan and over all cost and %cpu is showing higher than plan with Index skip scan. It is also using Index range scan after creating index on create_dt
.
I could not come to conclusion which should be could? Do I need to create another Index on create_dt
or is Index skip scan good? I believe Index skip is a feature of Oracle to run multiple index range scan?
Upvotes: 2
Views: 20140
Reputation: 36097
I recommend you to familiarize yourself with this link: http://docs.oracle.com/cd/E16655_01/server.121/e15858/tgsql_optop.htm#CHDFJIJA
It is Oracle 12c related, however it is very usefull to gain understanding how oracle uses different index access pathes in all DBMS versions.
Your subquery is ambigous:
select max(create_dt) from test_A where test_A.id2=id2
both test_A.id2 and id2 references to the same test_A.id2, and the query is equivalent to this:
select * from test_A where id2=123 and
create_dt=(select max(create_dt) from test_A where id2=id2);
or simply:
select * from test_A where id2=123 and
create_dt=(select max(create_dt) from test_A where id2 is not null);
I suppose that you want something like this:
select * from test_A where id2=123 and
create_dt=(select max(create_dt)
from test_A ALIAS
where test_A.id2=ALIAS.id2);
For the above query a composite index on id2+create_dt most likely give the best results, try it:
CREATE INDEX index_name ON test_A( id2, create_dt);
Upvotes: 3