I have a query with 'or' statement and 'inner select' statement. When I check query plan it uses full table access and I don't know why this happens.
I created this test case to show my problem:
CREATE TABLE PERSON (
ID NUMBER(18) NOT NULL,
NAME VARCHAR2(18) NOT NULL,
SURNAME VARCHAR2(18) NOT NULL
);
BEGIN
FOR b IN 1..500000
LOOP
INSERT INTO PERSON VALUES (b,'name' || to_char(b),'surname' || to_char(b));
END LOOP;
END;
CREATE INDEX PERSON_NAME_index ON PERSON(NAME);
CREATE INDEX PERSON_SURNAME_index ON PERSON(SURNAME);
And here is the problematic query:
SELECT p.*
FROM PERSON p
WHERE p.NAME = 'name300' or p.SURNAME in (SELECT p2.SURNAME
FROM PERSON p2
WHERE p2.NAME = 'name500');
When I analyze the query plan, I saw that it uses full table access for person table. That slows my query time drastically and I dont know why this happens.
If I eliminate first constraint(p.NAME = 'name300') and query with only subselect statement, everthing works fine and query uses indices again.
Can someone explain me why query is not using my indices for the first case?