我一直在尝试通过添加多个索引来优化查询。我在大多数人中取得了成功,但是有一个查询,我创建的列从未使用过。查询看起来像这样:
SELECT DISTINCT
c.containername
, hml.qty
, CAST(hml.txndate AS TIMESTAMP) txndate
, hml.comments
, e.fullname
, 'ICO' txn
FROM con c
JOIN hml hml ON c.containerid = hml.historyid
JOIN emp e ON hml.employeeid = e.employeeid
WHERE hml.comments LIKE '%ICO%'
AND hml.txndate >= to_date(:ddate, 'MM/DD/YYYY HH:MI:SS PM')
AND e.notes IN ('DBP', 'FTD', 'MH', 'SUPERVISOR')
ORDER BY CAST(hml.txndate AS TIMESTAMP);
执行计划
Plan hash value: 267728100
---------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 116 | 14616 | 248K (1)| 00:49:40 |
| 1 | SORT ORDER BY | | 116 | 14616 | 248K (1)| 00:49:40 |
| 2 | HASH UNIQUE | | 116 | 14616 | 248K (1)| 00:49:40 |
| 3 | NESTED LOOPS | | | | | |
| 4 | NESTED LOOPS | | 116 | 14616 | 248K (1)| 00:49:40 |
|* 5 | HASH JOIN | | 116 | 11252 | 248K (1)| 00:49:37 |
|* 6 | TABLE ACCESS FULL | EMPLOYEE | 8 | 304 | 25 (0)| 00:00:01 |
|* 7 | TABLE ACCESS BY INDEX ROWID| HISTORYMAINLINE | 38373 | 2210K| 248K (1)| 00:49:37 |
|* 8 | INDEX RANGE SCAN | HISTORYMAINLINEMX005 | 347K| | 1248 (1)| 00:00:15 |
|* 9 | INDEX UNIQUE SCAN | CONTAINER450 | 1 | | 1 (0)| 00:00:01 |
| 10 | TABLE ACCESS BY INDEX ROWID | CONTAINER | 1 | 29 | 2 (0)| 00:00:01 |
---------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
5 - access("HML"."EMPLOYEEID"="E"."EMPLOYEEID")
6 - filter("E"."NOTES"='DBP' OR "E"."NOTES"='FTD' OR "E"."NOTES"='MH' OR
"E"."NOTES"='SUPERVISOR')
7 - filter("HML"."COMMENTS" IS NOT NULL AND "HML"."COMMENTS" LIKE '%ICO%')
8 - access("HML"."TXNDATE">=TO_DATE(:DDATE,'MM/DD/YYYY HH:MI:SS AM'))
9 - access("C"."CONTAINERID"="HML"."HISTORYID")
费用为248,283
。与我投入指数之前的成本相同。此外,当我将hml.comments LIKE '%ICO%'
更改为hml.comments = '%ICO%'
时,费用会很好地提高(only 14!)
,因为我使用了创建的索引。不幸的是,我仍然需要使用LIKE,因为我不知道在评论栏中使用标签ICO的位置。
有没有办法可以改进查询?我正在使用Oracle 11g,而hml table
总共有40,193,106
行。提前感谢所有需要时间回答的人。