1
我們有很多使用綁定變量的選擇語句,這些語句可能爲空。 Null表示綁定值不應限制該語句。在SQL語句中連接綁定變量(Oracle 11g)
下面是一個簡單的例子,我們在做什麼:
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 |
-----------------------------------------------------------------------------
那麼,你認爲什麼是這種外部的最佳解決方案-joins?
我在查詢中看不到任何連接,更不用說外連接了?所以我不太瞭解這個問題。 – Codo
對不起,我使用「外連接」的意思是,綁定值必須「存在」或「不存在」 –