Question
Same query with same execution plan performs different in different Oracle environments
I have the following query:
SELECT t.*
FROM
(SELECT t.id, t.transaction_date
FROM transactions t
ORDER BY t.transaction_date DESC, t.id DESC
FETCH NEXT 11 ROWS ONLY) transactions_table
JOIN
transactions t ON transactions_table.id = t.id
ORDER BY
t.transaction_date DESC, t.id DESC;
ID
column is the PRIMARY KEY
for the table, I have the following CREATE INDEX
statement:
CREATE INDEX transaction_date_idx ON transactions (transaction_date DESC, id);
The execution plan is as follows:
PLAN_TABLE_OUTPUT
Plan hash value: 3772986339
---------------------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 11 | 167K| | 35981 (2)| 00:00:02 |
| 1 | SORT ORDER BY | | 11 | 167K| | 35981 (2)| 00:00:02 |
| 2 | NESTED LOOPS | | 11 | 167K| | 35980 (2)| 00:00:02 |
| 3 | NESTED LOOPS | | 11 | 167K| | 35980 (2)| 00:00:02 |
|* 4 | VIEW | | 11 | 286 | | 35958 (2)| 00:00:02 |
|* 5 | WINDOW SORT PUSHED RANK | | 4345K| 107M| 150M| 35958 (2)| 00:00:02 |
| 6 | INDEX FAST FULL SCAN | TRANSACTIONS_TRANSACTION_DATE_IDX | 4345K| 107M| | 3593 (2)| 00:00:01 |
|* 7 | INDEX UNIQUE SCAN | PK_TRANSACTIONS | 1 | | | 1 (0)| 00:00:01 |
| 8 | TABLE ACCESS BY INDEX ROWID| TRANSACTIONS | 1 | 15582 | | 2 (0)| 00:00:01 |
---------------------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
" 4 - filter(""from$_subquery$_003"".""rowlimit_$$_rownumber""<=11)"
" 5 - filter(ROW_NUMBER() OVER ( ORDER BY SYS_OP_DESCEND(""TRANSACTION_DATE""),INTERNAL_FUNCTION(""T"".""ID"") DESC "
)<=11)
" 7 - access(""T2"".""ID""=""from$_subquery$_003"".""ID"")"
Note
-----
- dynamic statistics used: dynamic sampling (level=2)
I have 2 environments with the same data (~4 million rows in transactions
table). The first environment the query runs as expected (~500ms). In the second environment it takes about 30 seconds! The execution plan for the both environments are exactly the same, except for the:
Note
-----
- dynamic statistics used: dynamic sampling (level=2)
not printed on the fast-returning environment. When I compare the inner queries:
SELECT t.id, t.transaction_date
FROM transactions t
ORDER BY t.transaction_date DESC, t.id DESC
FETCH NEXT 11 ROWS ONLY
it performs nearly the same in both environments (both fast). I believe things get slow when I run the JOIN
part. Why?