Query Optimization - Not In

Query Optimization - Not In

NOT IN clause in Oracle is very expensive and let the optimizer FULL 
Scan your tables. Because NOT IN does not use a limiting condition.

A way to overcome this problem is to rewrite the query with OUTER JOIN.
Outer Joins adds a limiting condition to your queries that can reduce 
the number of full table scans necessary
 

Example of NOT IN
-----------------------------
SELECT * FROM MYEMP 
WHERE DEPTNO NOT IN 
(
 SELECT DEPTNO FROM MYDEPT
);


Above query with Outer Join
-----------------------------
SELECT MYEMP.* FROM MYEMP, MYDEPT 
WHERE MYEMP.DEPTNO = MYDEPT.DEPTNO(+) 
AND MYDEPT.DEPTNO IS NULL;



Get More Query Optimization Tips:
http://nimishgarg.blogspot.in/2012/10/query-optimization-tips-for-oracle.html

6 comments:

  1. Hi ,

    The above join query,wil it possible to write it by using EXISTS operator ..?

    Plz let me know..???

    ReplyDelete
    Replies
    1. SELECT * FROM MYEMP e
      WHERE NOT exists
      (
      SELECT 1 FROM MYDEPT d where e.DEPTNO = d.DEPTNO
      );

      Delete
  2. But join works efficiently (especially when the columns have index on them) than EXISTS since exists will check for each and every rows individually.

    ReplyDelete
  3. how can we compare both query as not in means not equal(not match) and join means equal(same match)please explain

    ReplyDelete
  4. SQL – IN, NOT IN Operatörü ve Kullanımı – Ders 14
    Operatörler ve SQL’deki kullanımları ile ilgili anlatımlara IN, NOT IN operatörlerini anlatarak devam edelim. Efektif bir şekilde kullandığımızda işimize yarayacak bu operatörlerin syntaxı ve ne gibi problemlerde kullanılacağını yazıda detaylı olarak anlatmaya çalıştım.
    https://www.dogushan.com/sql-in-not-in-operatoru-ve-kullanimi-ders-14/

    ReplyDelete