dbcc dropcleanbuffers
select supplier_no from supplier
where supplier_no not in (select supplier_no from orders)
option (maxdop 1)
This runs for about three seconds and returns the following execution plan:
dbcc dropcleanbuffers
select supplier_no from supplier a
where not exists (select 1 from orders b
where a.supplier_no = b.supplier_no )
option (maxdop 1)
Again, this runs for about three seconds and returns the following execution plan:
dbcc dropcleanbuffers
select a.supplier_no
from supplier a
left join orders b on a.supplier_no = b.supplier_no
where b.supplier_no is null
option (maxdop 1)
Sticking with what we've seen previously, this also runs for about three seconds and produces the following execution plan:
dbcc dropcleanbuffers
select supplier_no from supplier
except
select supplier_no from orders
option (maxdop 1)
As before, the query takes about 3 seconds to run and returns the following execution plan:
SQL> select supplier_no from supplier
2 where supplier_no not in (select supplier_no from orders);
Generates the following plan:
Execution Plan
----------------------------------------------------------
Plan hash value: 1005948720
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 15421 | 210K| 572 (2)| 00:00:07 |
|* 1 | HASH JOIN ANTI | | 15421 | 210K| 572 (2)| 00:00:07 |
| 2 | INDEX FAST FULL SCAN| SUPPLIER_PK | 15421 | 105K| 10 (0)| 00:00:01 |
| 3 | INDEX FAST FULL SCAN| ORDERS_IX1 | 422K| 2884K| 559 (1)| 00:00:07 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("SUPPLIER_NO"="SUPPLIER_NO")
SQL> select supplier_no from supplier a
2 where not exists (select 1 from orders b
3 where a.supplier_no = b.supplier_no );
Produces this plan:
Execution Plan
----------------------------------------------------------
Plan hash value: 1005948720
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 15421 | 210K| 572 (2)| 00:00:07 |
|* 1 | HASH JOIN ANTI | | 15421 | 210K| 572 (2)| 00:00:07 |
| 2 | INDEX FAST FULL SCAN| SUPPLIER_PK | 15421 | 105K| 10 (0)| 00:00:01 |
| 3 | INDEX FAST FULL SCAN| ORDERS_IX1 | 422K| 2884K| 559 (1)| 00:00:07 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("A"."SUPPLIER_NO"="B"."SUPPLIER_NO")
Our third query (LEFT JOIN):
SQL> select a.supplier_no
2 from supplier a
3 left join orders b on a.supplier_no = b.supplier_no
4 where b.supplier_no is null;
Produces this plan:
Execution Plan
----------------------------------------------------------
Plan hash value: 1005948720
-------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes | Cost (%CPU)| Time |
-------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 15421 | 210K| 572 (2)| 00:00:07 |
|* 1 | HASH JOIN ANTI | | 15421 | 210K| 572 (2)| 00:00:07 |
| 2 | INDEX FAST FULL SCAN| SUPPLIER_PK | 15421 | 105K| 10 (0)| 00:00:01 |
| 3 | INDEX FAST FULL SCAN| ORDERS_IX1 | 422K| 2884K| 559 (1)| 00:00:07 |
-------------------------------------------------------------------------------------
Predicate Information (identified by operation id):
---------------------------------------------------
1 - access("A"."SUPPLIER_NO"="B"."SUPPLIER_NO")
FInally, the fourth one, replaces the SQL Server extension EXCEPT with the Oracle extension MINUS - otherwise they are identical.
SQL> select supplier_no from supplier
2 minus
3 select supplier_no from orders;
Finally, this plan is generated:
Execution Plan
----------------------------------------------------------
Plan hash value: 116939239
----------------------------------------------------------------------------------------------
| Id | Operation | Name | Rows | Bytes |TempSpc| Cost (%CPU)| Time |
----------------------------------------------------------------------------------------------
| 0 | SELECT STATEMENT | | 15421 | 2990K| | 2089 (2)| 00:00:26 |
| 1 | MINUS | | | | | | |
| 2 | SORT UNIQUE | | 15421 | 105K| 256K| 66 (4)| 00:00:01 |
| 3 | INDEX FAST FULL SCAN| SUPPLIER_PK | 15421 | 105K| | 10 (0)| 00:00:01 |
| 4 | SORT UNIQUE | | 422K| 2884K| 6632K| 2023 (2)| 00:00:25 |
| 5 | INDEX FAST FULL SCAN| ORDERS_IX1 | 422K| 2884K| | 559 (1)| 00:00:07 |
----------------------------------------------------------------------------------------------
Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.
Comments (0)