SlideShare a Scribd company logo
I want to share a small comparison between NOT EXISTS and NOT IN operator. We see these 2 operators
mostly in the sub-query and in reporting query. Few points that we should understand regarding these
operations:
1. Not In and Not Exists will produces exactly the same result if there is no NULL value in the sub-query. If
we have NULL value in the sub-query, we can add one more condition to eliminate that value (for
example: … AND (column_name IS NOT NULL …). The other approach is to define the column as
NOT NULL.
2. Not Exists is more efficient because when Oracle found 1 record in the sub-query that satisfies the
condition, Oracle will move to the next record of main query. If there is efficient index access to that
sub-query, Oracle will be able to use that index.
3. In case of Not In, Oracle has to scan the whole population (full table scan) of sub-query before Oracle
can move to the next record of main query,it requires more IO. If there is an efficient index access to
the sub-query, Oracle will not be able to use that index because of implicit LNNVL function in the
predicate.
4. Not In will be translated as ((column_name != VALUE_1) AND (column_name != VALUE_2)
AND (column_name != VALUE_3) … AND (column_name != VALUE_N)).
Let’s the party started! This is the test data.

We have 2 exactly the same tables (structure and data). BULK_ACT will be the main table, while BULK_ACT will
be the sub-query table. There is an index on each table. Later we update 2 records in the main table to
simulate NULL value and 1 different value between main and sub-query table.
This is the session statistics comparison (XLS) of those 2 operations and also the output of trace file of event
10053.
exist_vs_in.xlsx

In Predicate Information section of Not In version, we can see 1 bind variable, :B1, which can be translated as
Oracle executes the sub-query over and over again. We have 100,000 rows in BULK_ACT, so we will have
100,000 times Table Full Scan against BULK_GRP. Let’s check the session statistics for the confirmation.

There should be only 2 table scans in the Not Exists version, but we got 7 in above table, so the other 5 table
scans are belong to another tables/ processes. Looking forward to the Not In version, we got 100,006 table
scans, if 5 of its are belong to another tables, then we have only 100,001 table scans for the Not In version. 1
table scan for BULK_ACT and 100,000 table scans for BULK_GRP. The next 2 statistics (table scan blocks gotten
and table scan rows gotten) also confirmed that more operation were done in the Not In version.
Due to this huge table scans execution, the execution time and number of consistent gets for Not In version
also increase significantly.
Not In version with
dynamic sampling is on

Not Exists versionwith
dynamic sampling is on
So, what will be happened if we remove the “IS NOT NULL” part? The null value will be reported in the query as
below. There 2 rows in the output: 100,001 and NULL.

As mentioned earlier, we can also define the column as NOT NULL to have the same result.
What’s the Improvement in 11g?
There is new method which introduced in 11g to handle the inefficient Not In version (without re-writing the
query). I rerun the Not In version in 11g and here is the result.
As seen below, the new “HASH JOIN ANTI NA” has been introduced. The NA is stand for Null-Aware (please
read: http://structureddata.org/2008/05/22/null-aware-anti-join/). So we don’t need to rewrite the query 
since Oracle can take the benefit of Hash Join, instead of using Filter (as we have seen in previous 10g version).
The new
HASH JOIN ANTI NA

In above link, you will see also another new Hash Join method, “HASH JOIN ANTI SNA”. So please go through
that site, read and get your hand dirt with trial and error 
-heri-

More Related Content

DOCX
10053 - null is not nothing
Heribertus Bramundito
 
DOCX
The internals
Heribertus Bramundito
 
PPTX
Introduction to oracle optimizer
Heribertus Bramundito
 
DOCX
Correlated update vs merge
Heribertus Bramundito
 
PPTX
MYSql manage db
Ahmed Farag
 
PPTX
MYSQL join
Ahmed Farag
 
PPTX
Sql modifying data - MYSQL part I
Ahmed Farag
 
PPTX
MYSQL using set operators
Ahmed Farag
 
10053 - null is not nothing
Heribertus Bramundito
 
The internals
Heribertus Bramundito
 
Introduction to oracle optimizer
Heribertus Bramundito
 
Correlated update vs merge
Heribertus Bramundito
 
MYSql manage db
Ahmed Farag
 
MYSQL join
Ahmed Farag
 
Sql modifying data - MYSQL part I
Ahmed Farag
 
MYSQL using set operators
Ahmed Farag
 

What's hot (20)

PPSX
Data Structure (Circular Linked List)
Adam Mukharil Bachtiar
 
PPT
linked list
Narendra Chauhan
 
PDF
Circular linked list
maamir farooq
 
PPT
Unit ii(dsc++)
Durga Devi
 
PPTX
MYSQL single rowfunc-multirowfunc-groupby-having
Ahmed Farag
 
PPT
Linked list
eShikshak
 
DOCX
Technical
ved prakash
 
PPTX
Ppt on Linked list,stack,queue
Srajan Shukla
 
PPT
Circular linked list
dchuynh
 
PPTX
MySQL index optimization techniques
kumar gaurav
 
PPTX
Linklist
SHEETAL WAGHMARE
 
PPTX
Doubly & Circular Linked Lists
Afaq Mansoor Khan
 
PPTX
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
PPTX
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
PPT
linked list
Shaista Qadir
 
DOC
Sql Queries
webicon
 
PPT
IBM Informix Database SQL Set operators and ANSI Hash Join
Ajay Gupte
 
PDF
Singly linked list
Amar Jukuntla
 
PPTX
Bca ii dfs u-2 linklist,stack,queue
Rai University
 
PDF
Discover the power of Recursive SQL and query transformation with Informix da...
Ajay Gupte
 
Data Structure (Circular Linked List)
Adam Mukharil Bachtiar
 
linked list
Narendra Chauhan
 
Circular linked list
maamir farooq
 
Unit ii(dsc++)
Durga Devi
 
MYSQL single rowfunc-multirowfunc-groupby-having
Ahmed Farag
 
Linked list
eShikshak
 
Technical
ved prakash
 
Ppt on Linked list,stack,queue
Srajan Shukla
 
Circular linked list
dchuynh
 
MySQL index optimization techniques
kumar gaurav
 
Doubly & Circular Linked Lists
Afaq Mansoor Khan
 
Mca ii dfs u-3 linklist,stack,queue
Rai University
 
Data Structures - Lecture 9 [Stack & Queue using Linked List]
Muhammad Hammad Waseem
 
linked list
Shaista Qadir
 
Sql Queries
webicon
 
IBM Informix Database SQL Set operators and ANSI Hash Join
Ajay Gupte
 
Singly linked list
Amar Jukuntla
 
Bca ii dfs u-2 linklist,stack,queue
Rai University
 
Discover the power of Recursive SQL and query transformation with Informix da...
Ajay Gupte
 
Ad

Similar to Not in vs not exists (20)

DOCX
Nested loop join technique - part2
Heribertus Bramundito
 
PDF
Speeding up queries with semi joins and anti-joins
Kaing Menglieng
 
DOCX
Hash join
Heribertus Bramundito
 
PDF
Oracle SQL Part 3
Gurpreet singh
 
PDF
Application sql issues_and_tuning
Anil Pandey
 
DOCX
Interview Preparation
Ravi Kanudawala
 
PPT
Myth busters - performance tuning 102 2008
paulguerin
 
PPTX
Guide To Mastering The MySQL Query Execution Plan
Optimiz DBA
 
DOCX
Sql interview questions
nagesh Rao
 
PPT
SQL subquery
Vikas Gupta
 
PDF
SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343
Edgar Alejandro Villegas
 
PDF
Autonumber
oracle documents
 
PDF
How to Use VLOOKUP in Excel
Milorad Krstevski
 
PDF
UPD_OP_SQL
Valapet Badri
 
PDF
Migration
oracle documents
 
PPTX
Oracle interview questions
barbie0909
 
PDF
Oracle SQL Part 2
Gurpreet singh
 
DOCX
Dbms important questions and answers
LakshmiSarvani6
 
PPTX
Oracle foreign key missing index - a single index can boost performance
Carlos Oliveira
 
PDF
Ijetr012023
ER Publication.org
 
Nested loop join technique - part2
Heribertus Bramundito
 
Speeding up queries with semi joins and anti-joins
Kaing Menglieng
 
Oracle SQL Part 3
Gurpreet singh
 
Application sql issues_and_tuning
Anil Pandey
 
Interview Preparation
Ravi Kanudawala
 
Myth busters - performance tuning 102 2008
paulguerin
 
Guide To Mastering The MySQL Query Execution Plan
Optimiz DBA
 
Sql interview questions
nagesh Rao
 
SQL subquery
Vikas Gupta
 
SQL – The Natural Language for Analysis - Oracle - Whitepaper - 2431343
Edgar Alejandro Villegas
 
Autonumber
oracle documents
 
How to Use VLOOKUP in Excel
Milorad Krstevski
 
UPD_OP_SQL
Valapet Badri
 
Migration
oracle documents
 
Oracle interview questions
barbie0909
 
Oracle SQL Part 2
Gurpreet singh
 
Dbms important questions and answers
LakshmiSarvani6
 
Oracle foreign key missing index - a single index can boost performance
Carlos Oliveira
 
Ijetr012023
ER Publication.org
 
Ad

Recently uploaded (20)

PPTX
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
PDF
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
PDF
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
PPTX
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
PDF
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
PDF
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
PDF
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
PDF
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
PDF
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
PDF
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
PPTX
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
PPTX
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
PPTX
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
PDF
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
PPTX
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
PPTX
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
PPTX
The Future of AI & Machine Learning.pptx
pritsen4700
 
PDF
Doc9.....................................
SofiaCollazos
 
PPTX
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
PDF
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 
Applied-Statistics-Mastering-Data-Driven-Decisions.pptx
parmaryashparmaryash
 
NewMind AI Weekly Chronicles - July'25 - Week IV
NewMind AI
 
Get More from Fiori Automation - What’s New, What Works, and What’s Next.pdf
Precisely
 
New ThousandEyes Product Innovations: Cisco Live June 2025
ThousandEyes
 
A Strategic Analysis of the MVNO Wave in Emerging Markets.pdf
IPLOOK Networks
 
How ETL Control Logic Keeps Your Pipelines Safe and Reliable.pdf
Stryv Solutions Pvt. Ltd.
 
Responsible AI and AI Ethics - By Sylvester Ebhonu
Sylvester Ebhonu
 
The Future of Mobile Is Context-Aware—Are You Ready?
iProgrammer Solutions Private Limited
 
Google I/O Extended 2025 Baku - all ppts
HusseinMalikMammadli
 
Data_Analytics_vs_Data_Science_vs_BI_by_CA_Suvidha_Chaplot.pdf
CA Suvidha Chaplot
 
AI in Daily Life: How Artificial Intelligence Helps Us Every Day
vanshrpatil7
 
Introduction to Flutter by Ayush Desai.pptx
ayushdesai204
 
Dev Dives: Automate, test, and deploy in one place—with Unified Developer Exp...
AndreeaTom
 
Security features in Dell, HP, and Lenovo PC systems: A research-based compar...
Principled Technologies
 
The-Ethical-Hackers-Imperative-Safeguarding-the-Digital-Frontier.pptx
sujalchauhan1305
 
AI and Robotics for Human Well-being.pptx
JAYMIN SUTHAR
 
The Future of AI & Machine Learning.pptx
pritsen4700
 
Doc9.....................................
SofiaCollazos
 
cloud computing vai.pptx for the project
vaibhavdobariyal79
 
Accelerating Oracle Database 23ai Troubleshooting with Oracle AHF Fleet Insig...
Sandesh Rao
 

Not in vs not exists

  • 1. I want to share a small comparison between NOT EXISTS and NOT IN operator. We see these 2 operators mostly in the sub-query and in reporting query. Few points that we should understand regarding these operations: 1. Not In and Not Exists will produces exactly the same result if there is no NULL value in the sub-query. If we have NULL value in the sub-query, we can add one more condition to eliminate that value (for example: … AND (column_name IS NOT NULL …). The other approach is to define the column as NOT NULL. 2. Not Exists is more efficient because when Oracle found 1 record in the sub-query that satisfies the condition, Oracle will move to the next record of main query. If there is efficient index access to that sub-query, Oracle will be able to use that index. 3. In case of Not In, Oracle has to scan the whole population (full table scan) of sub-query before Oracle can move to the next record of main query,it requires more IO. If there is an efficient index access to the sub-query, Oracle will not be able to use that index because of implicit LNNVL function in the predicate. 4. Not In will be translated as ((column_name != VALUE_1) AND (column_name != VALUE_2) AND (column_name != VALUE_3) … AND (column_name != VALUE_N)). Let’s the party started! This is the test data. We have 2 exactly the same tables (structure and data). BULK_ACT will be the main table, while BULK_ACT will be the sub-query table. There is an index on each table. Later we update 2 records in the main table to simulate NULL value and 1 different value between main and sub-query table. This is the session statistics comparison (XLS) of those 2 operations and also the output of trace file of event 10053.
  • 2. exist_vs_in.xlsx In Predicate Information section of Not In version, we can see 1 bind variable, :B1, which can be translated as Oracle executes the sub-query over and over again. We have 100,000 rows in BULK_ACT, so we will have 100,000 times Table Full Scan against BULK_GRP. Let’s check the session statistics for the confirmation. There should be only 2 table scans in the Not Exists version, but we got 7 in above table, so the other 5 table scans are belong to another tables/ processes. Looking forward to the Not In version, we got 100,006 table scans, if 5 of its are belong to another tables, then we have only 100,001 table scans for the Not In version. 1 table scan for BULK_ACT and 100,000 table scans for BULK_GRP. The next 2 statistics (table scan blocks gotten and table scan rows gotten) also confirmed that more operation were done in the Not In version. Due to this huge table scans execution, the execution time and number of consistent gets for Not In version also increase significantly.
  • 3. Not In version with dynamic sampling is on Not Exists versionwith dynamic sampling is on
  • 4. So, what will be happened if we remove the “IS NOT NULL” part? The null value will be reported in the query as below. There 2 rows in the output: 100,001 and NULL. As mentioned earlier, we can also define the column as NOT NULL to have the same result.
  • 5. What’s the Improvement in 11g? There is new method which introduced in 11g to handle the inefficient Not In version (without re-writing the query). I rerun the Not In version in 11g and here is the result. As seen below, the new “HASH JOIN ANTI NA” has been introduced. The NA is stand for Null-Aware (please read: http://structureddata.org/2008/05/22/null-aware-anti-join/). So we don’t need to rewrite the query  since Oracle can take the benefit of Hash Join, instead of using Filter (as we have seen in previous 10g version).
  • 6. The new HASH JOIN ANTI NA In above link, you will see also another new Hash Join method, “HASH JOIN ANTI SNA”. So please go through that site, read and get your hand dirt with trial and error  -heri-