我有一個簡單的查詢正在運行。有一個日期條件,我一旦刪除,查詢就返回結果。它的格式爲'31 -MAR-15'的日期字段。我不明白爲什麼這種情況會使查詢速度變慢。提前致謝。Oracle - 查詢運行速度很慢
SELECT
substr(a.id, 1, 2) AS country,
count(DISTINCT a.id) AS id_count,
sum(a.amount) AS amount
FROM table1 a
JOIN table2 b ON a.id = b.id
JOIN table3 c ON b.party_id = c.party_id
WHERE a.prod_type = 'INS'
AND c.acct_type = 'LON'
AND substr(a.id, 1, 2) = 'US'
AND a.dump_dt = '31-MAR-15'
AND substr(id, 4, 8) = '20150303'
GROUP BY substr(a.id, 1, 2);
解釋計劃:
PLAN_TABLE_OUTPUT
Plan hash value: 255044277
------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 1 | 121 | 125K (1)| 00:25:08 |
| 1 | HASH GROUP BY | | 1 | 121 | 125K (1)| 00:25:08 |
| 2 | VIEW | VW_DAG_0 | 1 | 121 | 125K (1)| 00:25:08 |
| 3 | HASH GROUP BY | | 1 | 98 | 125K (1)| 00:25:08 |
| 4 | NESTED LOOPS | | | | | |
| 5 | NESTED LOOPS | | 1 | 98 | 125K (1)| 00:25:08 |
| 6 | MERGE JOIN CARTESIAN | | 12613 | 800K| 21133 (2)| 00:04:14 |
|* 7 | TABLE ACCESS BY INDEX ROWID| TABLE1 | 1 | 45 | 46 (0)| 00:00:01 |
|* 8 | INDEX RANGE SCAN | DATA_DATE__STG_BACKUP2 | 1040 | | 6 (0)| 00:00:01 |
| 9 | BUFFER SORT | | 182K| 3564K| 21087 (2)| 00:04:14 |
|* 10 | TABLE ACCESS FULL | TABLE3 | 182K| 3564K| 21087 (2)| 00:04:14 |
|* 11 | INDEX RANGE SCAN | BSB_PARTYID_IDX | 22 | | 3 (0)| 00:00:01 |
|* 12 | TABLE ACCESS BY INDEX ROWID | TABLE2 | 1 | 33 | 10 (0)| 00:00:01 |
------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
7-filter(SUBSTR(A.ID, 4, 8) = '20150303' AND SUBSTR(A.ID, 1, 2) = 'US'
AND A.PROD_TYPE = 'INS')
8 - access(A.DUMP_DT = '31-MAR-15')
10 - filter(C.ACCT_TYPE = 'LON')
11 – access(B.PARTY_ID = C.PARTY_ID)
12 - filter(A.ID = B.ID)
什麼'dump_dt'的類型?你有什麼指數?什麼是解釋計劃? – Mat
您是否檢查過DUMP_DT上有索引? – PhillipD
這對Oracle來說是無效的SQL,它不支持用於別名表的'AS'關鍵字。有些東西你沒有告訴我們......正如其他人所說的DDL表和解釋計劃是_essential_爲了讓你得到一個很好的答案,它看起來好像你將日期存儲爲字符串,這總是一個配方災難和最後'SUBSTR(ID,4,9)'返回9個字符,而不是8,所以除非ID少於13個字符長度,我期望'SUBSTR(ID,4,9)='20150303'始終返回什麼也沒有,這意味着你的查詢不會返回任何東西你能否澄清你的問題? – Ben