Reduce the result set as early as possible | IN versus Exists


Avoid using sub-selects and redundant expressions

Let us look at the following real example on subselects:.

SELECT distinct(CATENTRY_ID)
from CATENTRY 
where MARKFORDELETE=0 
AND 
1)  (CATENTRY_ID
     in (SELECT CATENTRY_ID 
          FROM CATGPENREL 
         WHERE CATGROUP_ID = 10000000)
         or CATENTRY_ID       
2)      in (SELECT CATENTRY_ID_CHILD 
           from CATENTREL, CATGPENREL
          where CATENTRY_ID_PARENT=CATENTRY_ID 
           and CATGROUP_ID = 10000000))

The above SQL has two levels of subselect. This makes the SQL statement very expensive. Optimization is required for such statements. If there are common tables in the From clause between the sub-selects, then it is a good hint that the sub-selects can be converted easily to joins.

A better and more efficient version of the SQL is:

SELECT distinct (c.CATENTRY_ID)
From CATENTRY c , catgpenrel b ,catentrel d
Where 
  MARKFORDELETE=0
  AND
  b.CATGROUP_ID = 10000000
  and
  (
1)  (c.catentry_id=b.catentry_id)
        or
2)  (c.CATENTRY_ID=d.CATENTRY_ID_PARENT
     and b.catentry_id=d.CATENTRY_ID_CHILD))

Note that: