使用 coalesce(或 NVL)有一个好处,您可以利用 function-based indexes 来提高查询性能。
设置:
FSITJA@db01 2019-07-18 09:21:33> create table tblreqs (onum, pnum, reqnum primary key) as
2 with t (onum, pnum, reqnum) as (
3 select NULL, 'P427', 'RN1148' from dual union all
4 select NULL, 'P324', 'RN1725' from dual union all
5 select NULL, 'P229', 'RN1242' from dual union all
6 select 'O396', NULL, 'RN1457' from dual union all
7 select 'O380', NULL, 'RN1205' from dual union all
8 select 'O258', NULL, 'RN1482' from dual
9 ) select * from t;
Table created.
FSITJA@db01 2019-07-18 09:21:33> create table tblnums (nums primary key) as
2 with t (nums)as (
3 select 'O258' from dual union all
4 select 'O370' from dual union all
5 select 'O490' from dual union all
6 select 'O314' from dual union all
7 select 'O379' from dual union all
8 select 'P341' from dual union all
9 select 'P230' from dual union all
10 select 'P280' from dual union all
11 select 'P324' from dual union all
12 select 'P395' from dual
13 ) select * from t;
Table created.
请注意,由于条件连接(TBLREQS 上的 TABLE ACCESS STORAGE FULL),oracle 如何无法利用索引:
FSITJA@db01 2019-07-18 09:21:33> explain plan for
2 SELECT n.nums,
3 r.ReqNum
4 FROM tblnums n
5 LEFT JOIN tblReqs r ON n.nums = r.Onum OR n.nums = r.pnum;
Explained.
FSITJA@db01 2019-07-18 09:21:33> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 1571794044
-----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10 | 130 | 31 (0)| 00:00:01 |
| 1 | NESTED LOOPS OUTER | | 10 | 130 | 31 (0)| 00:00:01 |
| 2 | INDEX FULL SCAN | SYS_C0047401 | 10 | 50 | 1 (0)| 00:00:01 |
| 3 | VIEW | VW_LAT_EB747914 | 1 | 8 | 3 (0)| 00:00:01 |
|* 4 | TABLE ACCESS STORAGE FULL| TBLREQS | 1 | 13 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - filter("R"."ONUM" IS NOT NULL AND "N"."NUMS"="R"."ONUM" OR "R"."PNUM" IS NOT
NULL AND "N"."NUMS"="R"."PNUM")
17 rows selected.
现在,如果我们使用 coalesce 函数创建基于函数的索引,oracle 检测到该索引存在并将使用它来提高连接性能,而无需进行全表扫描(INDEX RANGE SCAN 对新创建的 IDX_COALESCE_ONUM_PNUM):
FSITJA@db01 2019-07-18 09:21:33> create index idx_coalesce_onum_pnum on tblreqs (coalesce(Onum, Pnum));
Index created.
FSITJA@db01 2019-07-18 09:21:33> explain plan for
2 SELECT n.nums,
3 r.ReqNum
4 FROM tblnums n
5 LEFT JOIN tblReqs r ON n.nums = COALESCE(r.Onum, r.Pnum);
Explained.
FSITJA@db01 2019-07-18 09:21:33> select * from table(dbms_xplan.display);
PLAN_TABLE_OUTPUT
------------------------------------------------------------------------------------------------------------------------------------
Plan hash value: 605824869
---------------------------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
---------------------------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 10 | 160 | 2 (0)| 00:00:01 |
| 1 | NESTED LOOPS OUTER | | 10 | 160 | 2 (0)| 00:00:01 |
| 2 | INDEX FULL SCAN | SYS_C0047401 | 10 | 50 | 1 (0)| 00:00:01 |
| 3 | TABLE ACCESS BY INDEX ROWID BATCHED| TBLREQS | 1 | 11 | 1 (0)| 00:00:01 |
|* 4 | INDEX RANGE SCAN | IDX_COALESCE_ONUM_PNUM | 1 | | 0 (0)| 00:00:01 |
---------------------------------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
4 - access("N"."NUMS"="R"."SYS_NC00004$"(+))
16 rows selected.