Why is there such a huge performance difference between these two queries?
-- (89 seconds)
SELECT max(mydate) FROM mytable WHERE eqpid = 'ABCDEFG'
AND mydate < sysdate - 5
vs.
-- (0.6 seconds)
SELECT max(mydate) FROM mytable WHERE eqpid = 'ABCDEFG'
AND mydate < TO_DATE('05/27/2011 03:13:00', 'MM/DD/YYYY HH24:MI:SS') -- 5 days ago
Regardless of indexes, it seems that both to_date and sysdate just return "some date value".
Notes: A composite index exists on this table including eqpid and 2 other columns. An index also exists for mydate. Both are b-tree. There are about 29 million rows.
Why would the optimizer choose such an obviously different (and in one case, awful) plan for these?
eqpid
? Is is a combined index witheqpid
down the list of columns? If so then Oracle might think that it isn't an efficient type index to use, and so it penalizes that plan. – Spindlelegs