Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

WHY In Clause Faster than EXISTS clause? 2

Status
Not open for further replies.

jdu2006

Programmer
May 13, 2008
6
US
I used to use the exists clause in my embedded sql statement and it was always working fine. But my process is suddenly chocked and I found out the problem was exists clause, if I changed the exists clause to in clause, the process would be resumed. Any idea? Please help and thanks in advance
 
Many things affect that.

Just a small example

select ...
from tbl1
where (fld1,fld2,fld3) in
(select fld1,fld2,fld3
from tbl2
)

suppose tbl1 had 20 million records, and tbl2 has 100k records, with some duplicates on the values of fld1,fld2,fld3 resulting on only 70k records being returned on the tbl2 select.
Also from the 20 million records of tbl1, only 2 million records satisfy the "in" clause.

DB2 will be smart enough to determine the better access path on this case, and if fld1,fld2,fld3 are a index (or at least the first 3 fields of an index) of tbl1, then it will be a lot faster than doing the EXISTS, as this one will read ALL index pages from TBL1 in order to access TBL2.

As i said this is just one possible explanation. All depends on your SQL, your table indexes, number of records in each that satisfy your SQL predicates, and on a few other things.




Regards

Frederico Fonseca
SysSoft Integrated Ltd

FAQ219-2884
FAQ181-2886
 
Great point. I will check my sql data and indexes and return you back. Thanks. John
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top