The following query has good performance
select distinct i.TERMINALNAME,
to_char(i.BEGINTIME,'mm/dd/yyyy hh:mi:ss AM') BEGINTIME,
i.ERRORTEXT,
i.RECORDSPROCESSED,
i.RECORDTYPE
from MYLOGGING i
inner join (select recordtype,
max(BEGINTIME) as lastrundate
from MYLOGGING group by recordtype) im on
im.recordtype=i.recordtype and im.lastrundate=i.BEGINTIME
where i.ERRORTEXT in ('Success', 'Failure')
and i.TERMINALNAME Not In ('REE300', 'XEE300', 'YT', 'QX', 'VC', 'DF')
ORDER BY i.TERMINALNAME ASC;
QUERY PLAN
Plan hash value: 3900617130
------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 2 | 132 | 1257 (2)| 00:00:01 |
| 1 | SORT UNIQUE | | 2 | 132 | 1256 (2)| 00:00:01 |
|* 2 | HASH JOIN RIGHT SEMI| | 2 | 132 | 1255 (2)| 00:00:01 |
| 3 | VIEW | | 772 | 16984 | 630 (2)| 00:00:01 |
| 4 | HASH GROUP BY | | 772 | 16212 | 630 (2)| 00:00:01 |
| 5 | TABLE ACCESS FULL| MYLOGGING | 281K| 5765K| 623 (1)| 00:00:01 |
|* 6 | TABLE ACCESS FULL | MYLOGGING | 191K| 8222K| 625 (1)| 00:00:01 |
------------------------------------------------------------------------------------------
However with the new where clause added at the end of the query and i.BEGINTIME>= trunc(sysdate) - 60
, it results the query to run much slower if running for the first time, after running it the second time it will be fast.
select distinct i.TERMINALNAME,
to_char(i.BEGINTIME,'mm/dd/yyyy hh:mi:ss AM') BEGINTIME,
i.ERRORTEXT,
i.RECORDSPROCESSED,
i.RECORDTYPE
from MYLOGGING i
inner join (select recordtype,
max(BEGINTIME) as lastrundate
from MYLOGGING group by recordtype) im on im.recordtype=i.recordtype
and im.lastrundate=i.BEGINTIME
where i.ERRORTEXT in ('Success', 'Failure')
and i.TERMINALNAME Not In ('REE300', 'XEE300', 'YT', 'QX', 'VC', 'DF')
and i.BEGINTIME>= trunc(sysdate) - 60 ORDER BY i.TERMINALNAME ASC;
Query Plan
Plan hash value: 2346866897
----------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
----------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 86 | 809 (1)| 00:00:01 |
| 1 | SORT UNIQUE | | 1 | 86 | 809 (1)| 00:00:01 |
|* 2 | FILTER | | | | | |
| 3 | SORT GROUP BY | | 1 | 86 | 809 (1)| 00:00:01 |
|* 4 | HASH JOIN | | 32572 | 2735K| 807 (1)| 00:00:01 |
|* 5 | TABLE ACCESS BY INDEX ROWID BATCHED| MYLOGGING | 1 | 64 | 4 (0)| 00:00:01 |
|* 6 | INDEX RANGE SCAN | IX2_MYLOGGING | 1 | | 3 (0)| 00:00:01 |
| 7 | TABLE ACCESS FULL | MYLOGGING | 283K| 6081K| 802 (1)| 00:00:01 |
----------------------------------------------------------------------------------------------------------------
The query plan shows that the query runs fast since it was not the first time I ran it and I assume the data is cached. I don't privilege to flush the cache.
Any idea what could be causing the query to run very slow the first time it's ran? More than 2-3 minutes. Currently the index on the BEGINTIME column has "join_index" to no. Could that have anything to do with it?