2012-08-08 34 views
1

我有以下查詢Oracle路口:有或無明顯

select id from t1 
intersect 
select id from t2 
intersect 
select id from t3 

ID可能是一些表的非唯一的,所以我需要使用不同的。

一般什麼是更好的:

select distinct id from (
select id from t1 
intersect 
select id from t2 
intersect 
select id from t3) 

select distinct id from t1 
intersect 
select id from t2 -- here id is unique 
intersect 
select distinct id from t3 
+1

「更好」 - 用什麼術語?性能?只需運行它們並找出或使用解釋計劃。 – Ollie 2012-08-08 09:00:54

+0

@奧莉,是的表現 – 2012-08-08 09:03:25

回答

4

沒有必要爲DISTINCTINTERSECT運算符自動生成一組不同的值。正如你可以看到在這個例子,x具有兩排爲1的IDy只有一排爲1的ID和兩個INTERSECTION產生一個單行

SQL> ed 
Wrote file afiedt.buf 

    1 with x as (select 1 id from dual union all select 1 from dual), 
    2  y as (select 1 id from dual) 
    3 select id 
    4 from x 
    5 intersect 
    6 select id 
    7* from y 
SQL>/

     ID 
---------- 
     1 

即使你拿INTERSECT與自己的兩排桌子,你仍然在輸出中獲得單排

SQL> ed 
Wrote file afiedt.buf 

    1 with x as (select 1 id from dual union all select 1 from dual) 
    2 select id 
    3 from x 
    4 intersect 
    5 select id 
    6* from x 
SQL>/

     ID 
---------- 
     1 
相關問題