we have a lot of Select Statements which use Bind-Variables that can be Null. Null means that the Bind-Value should not restrict the statement.
Here is a simple example what we are doing:
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);
The 2 Statements have the following execution plans:
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 |
-----------------------------------------------------------------------------
So, what do you think is the best solution for such outer-joins?