Why the second query is more efficient? [message #598277] |
Sat, 12 October 2013 02:17 |
|
zhaoquer
Messages: 35 Registered: October 2013
|
Member |
|
|
select .........
from TAB1
where COL1 like 'ABC%'
and COL2 NOT IN
(select FLD2 from TAB2 where FLD3 between '20130101' and '20130131');
select ........
from TAB1
where COL1 like 'ABC%'
and COL2 NOT EXISTS
(select FLD2
from TAB2
where FLD2 = COL2
AND FLD3 between '20130101' and '20130131');
|
|
|
|
|
|
Re: Why the second query is more efficient? [message #598288 is a reply to message #598285] |
Sat, 12 October 2013 03:35 |
|
Michel Cadot
Messages: 68645 Registered: March 2007 Location: Nanterre, France, http://...
|
Senior Member Account Moderator |
|
|
This is basic of SQL but often a misunderstood point.
dummy NOT IN (select ...)
means
dummy NOT IN (all the values returned by the query)
that is here
NOT IN ('Y',null)
which means
dummy <> 'Y' AND dummy <> null
this condition is TRUE (which is the condition for the row to be returned) only if the 2 sides of AND are TRUE but the second part with NULL can NEVER be TRUE, it is always UNKNOWN.
So the whole condition is always UNKNOWN (whatever the value of dummy column) and not TRUE and no rows are returned.
Database SQL Reference
[Updated on: Sat, 12 October 2013 05:17] Report message to a moderator
|
|
|
|
|
Re: Why the second query is more efficient? [message #598295 is a reply to message #598277] |
Sat, 12 October 2013 07:02 |
Lalit Kumar B
Messages: 3174 Registered: May 2013 Location: World Wide on the Web
|
Senior Member |
|
|
zhaoquer wrote on Sat, 12 October 2013 12:47
(select FLD2 from TAB2 where FLD3 between '20130101' and '20130131');
I am sure FLD3 is a DATE column. What is the data type, DATE or VARCHAR?
In your query, you will always be safe if you write :
Select FLD2 from TAB2 where FLD3 between to_date('20130101' , 'YYYYMMDD') and to_date('20130131' , 'YYYYMMDD')
Regards,
Lalit
|
|
|
|
|