![]() | |
![]() |
| | Thread Tools | Display Modes |
#11
| |||
| |||
|
|
Is there a standard SQL operator that will do this? |
#12
| |||
| |||
|
|
Is there a standard SQL operator that will do this? SQL-92 defined an OUTER UNION, but nobody implemented it. |
|
5 6 (6 rows) |
#13
| |||
| |||
|
#14
| |||
| |||
|
|
cdecarlo (AT) gmail (DOT) com wrote: Hello, I'm trying to determine the negation of A intersect B quicker than: (A UNION B) MINUS (A INTERSECT B) Is there a standard SQL operator that will do this? Thanks Probably in this way. At first createing some tables and feeding with data. SQL> CREATE TABLE a (a NUMBER); Table created. SQL> CREATE TABLE b (a NUMBER); Table created. SQL> INSERT INTO a SELECT rownum FROM all_objects WHERE rownum <6; 5 rows created. SQL> INSERT INTO b SELECT rownum + 3 FROM all_objects WHERE rownum <6; 5 rows created. SQL> SELECT * FROM a; A ---------- 1 2 3 4 5 SQL> SELECT * FROM b; A ---------- 4 5 6 7 8 Your query would be as follows: SQL> SELECT * FROM (( 2 SELECT * FROM a 3 UNION 4 SELECT * FROM b) 5 MINUS ( 6 SELECT * FROM a 7 INTERSECT 8 SELECT * FROM b 9 )); A ---------- 1 2 3 6 7 8 6 rows selected. Changed query: SQL> SELECT * FROM ( 2 SELECT DISTINCT a FROM a 3 UNION ALL 4 SELECT DISTINCT a FROM b 5 ) 6 GROUP BY a 7 HAVING count(*) = 1; A ---------- 1 2 3 6 7 8 6 rows selected. And now let's look at explain plans and statistics: SQL> set autotrace traceonly SQL> SELECT * FROM (( 2 SELECT * FROM a 3 UNION 4 SELECT * FROM b) 5 MINUS ( 6 SELECT * FROM a 7 INTERSECT 8 SELECT * FROM b 9 )); 6 rows selected. Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=FIRST_ROWS (Cost=1 Card=1 Bytes=1 3) 1 0 VIEW (Cost=1 Card=1 Bytes=13) 2 1 MINUS 3 2 SORT (UNIQUE) (Cost=60 Card=164 Bytes=4264) 4 3 UNION-ALL 5 4 TABLE ACCESS (FULL) OF 'A' (Cost=2 Card=82 Bytes=1 066) 6 4 TABLE ACCESS (FULL) OF 'B' (Cost=2 Card=82 Bytes=1 066) 7 2 INTERSECTION 8 7 SORT (UNIQUE) (Cost=15 Card=82 Bytes=1066) 9 8 TABLE ACCESS (FULL) OF 'A' (Cost=2 Card=82 Bytes=1 066) 10 7 SORT (UNIQUE) (Cost=15 Card=82 Bytes=1066) 11 10 TABLE ACCESS (FULL) OF 'B' (Cost=2 Card=82 Bytes=1 066) Statistics ---------------------------------------------------------- 0 recursive calls 0 db block gets 28 consistent gets 0 physical reads 0 redo size 467 bytes sent via SQL*Net to client 495 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 3 sorts (memory) 0 sorts (disk) 6 rows processed SQL> SELECT * FROM ( 2 SELECT DISTINCT a FROM a 3 UNION ALL 4 SELECT DISTINCT a FROM b 5 ) 6 GROUP BY a 7 HAVING count(*) = 1; 6 rows selected. Execution Plan ---------------------------------------------------------- 0 SELECT STATEMENT Optimizer=FIRST_ROWS (Cost=42 Card=164 Byte s=2132) 1 0 FILTER 2 1 SORT (GROUP BY) (Cost=42 Card=164 Bytes=2132) 3 2 VIEW (Cost=30 Card=164 Bytes=2132) 4 3 UNION-ALL 5 4 SORT (UNIQUE) (Cost=15 Card=82 Bytes=1066) 6 5 TABLE ACCESS (FULL) OF 'A' (Cost=2 Card=82 Bytes =1066) 7 4 SORT (UNIQUE) (Cost=15 Card=82 Bytes=1066) 8 7 TABLE ACCESS (FULL) OF 'B' (Cost=2 Card=82 Bytes =1066) Statistics ---------------------------------------------------------- 0 recursive calls 0 db block gets 14 consistent gets 0 physical reads 0 redo size 467 bytes sent via SQL*Net to client 495 bytes received via SQL*Net from client 2 SQL*Net roundtrips to/from client 3 sorts (memory) 0 sorts (disk) 6 rows processed SQL 4 full scans and 3 sorts vs 2 full scans and 3 sorts and 28 consistent gets vs 14 consistent gets. Of course I used DISTINCT just to show that in general case you should use it. Gints Plivna http://www.gplivna.eu/ |
#15
| |||
| |||
|
|
FULL OUTER JOIN <> OUTER UNION |
#16
| |||
| |||
|
|
"--CELKO--" <jcelko212 (AT) earthlink (DOT) net> wrote: FULL OUTER JOIN <> OUTER UNION Care to explain the difference? I thought that the second was simply a shorthand for the first. I think it is as follows... |
#17
| |||
| |||
|
#18
| |||
| |||
|
|
Given two tables, t1 with columns a and b, and t2 with columns c and d, FULL OUTER JOIN will produce a table with four columns, a,b,c and d, some of which will contain nulls where no match occurs between then tables. OUTER UNION will produce a table with two columns. The table will consist of those rows from t1 for which there is no matching row in t2, and those rows from t2 for which there is no matching row in t1. |
#19
| |||
| |||
|
|
"--CELKO--" <jcelko212 (AT) earthlink (DOT) net> wrote: FULL OUTER JOIN <> OUTER UNION Care to explain the difference? I thought that the second was simply a shorthand for the first. |
![]() |
| Thread Tools | |
| Display Modes | |
| |