Here is my query,
SELECT ID As Col1,
(
SELECT VID FROM TABLE2 t
WHERE (a.ID=t.ID or a.ID=t.ID2)
AND t.STARTDTE =
(
SELECT MAX(tt.STARTDTE)
FROM TABLE2 tt
WHERE (a.ID=tt.ID or a.ID=tt.ID2) AND tt.STARTDTE < SYSDATE
)
) As Col2
FROM TABLE1 a
Table1 has 48850 records and Table2 has 15944098 records.
I have separate indexes in TABLE2 on ID,ID & STARTDTE, STARTDTE, ID, ID2 & STARTDTE.
The query is still too slow. How can this be improved? Please help.
I’m guessing that the
ORin inner queries is messing up with the optimizer’s ability to use indexes. Also I wouldn’t recommend a solution that would scan all ofTABLE2given its size.This is why in this case I would suggest using a function that will efficiently retrieve the information you are looking for (2 index scan per call):
Your SQL would become:
Make sure you have indexes on both
table2(id, startdte DESC)andtable2(id2, startdte DESC). The order of the index is very important.