我们有很多使用可以为 Null 的绑定变量的选择语句。Null 表示 Bind-Value 不应限制语句。
这是我们正在做的一个简单示例:
CREATE TABLE PERSON AS
SELECT LEVEL AS ID, 'Person_'||LEVEL AS NAME
FROM DUAL
CONNECT BY LEVEL <= 5000;
create index IPERSON1 on PERSON(NAME, ID);
begin
dbms_stats.gather_table_stats(user, 'PERSON');
end;
select * from PERSON
where NAME = nvl(:b1, NAME);
select * from PERSON
where (NAME =:b1 or :b1 is null);
这 2 个语句具有以下执行计划:
select * from PERSON where NAME =nvl(:b1, NAME):
------------------------------------------------------------------------------------
| Id | Operation | Name | E-Rows |E-Bytes| Cost (%CPU)| E-Time |
------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 4 (100)| |
| 1 | CONCATENATION | | | | | |
|* 2 | FILTER | | | | | |
|* 3 | INDEX FAST FULL SCAN| IPERSON1 | 500 | 7500 | 2 (0)| 00:00:01 |
|* 4 | FILTER | | | | | |
|* 5 | INDEX RANGE SCAN | IPERSON1 | 1 | 15 | 2 (0)| 00:00:01 |
------------------------------------------------------------------------------------
select * from PERSON where (NAME =:b1 or :b1 is null):
-----------------------------------------------------------------------------
| Id | Operation | Name | E-Rows |E-Bytes| Cost (%CPU)| E-Time |
-----------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | | | 3 (100)| |
|* 1 | TABLE ACCESS FULL| PERSON | 26 | 390 | 3 (0)| 00:00:01 |
-----------------------------------------------------------------------------
那么,您认为这种外连接的最佳解决方案是什么?