SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


The Pitfall of "Not Equal To" Operator in Queries!


The Pitfall of "Not Equal To" Operator in Queries!

Author
Message
Dave F-425609
Dave F-425609
SSC Veteran
SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)

Group: General Forum Members
Points: 296 Visits: 37

Cheers for that Mike. I'm coming from an Oracle background and in older editions OUTER JOIN=Full Table Scan.

I spent a bit more time looking into EXISTS after my post, and see what you mean. When you code EXISTS rather than IN the first time it hits success it breaks the analysis loop. This will be quicker if you are bringing back a huge number of rows from the subquery.

HOWEVER, by the same rational NOT EXISTS should not be quicker than NOT IN AS it would have to scan the entire result set to ensure a single occurence isn't present. Seems to follow...


Mike C
Mike C
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2781 Visits: 1168

No problem Dave. There is a difference between NOT EXISTS and NOT IN in some cases. Consider the following sample:

CREATE TABLE #test (id INT PRIMARY KEY,
val VARCHAR(20));

INSERT INTO #test (id, val)
SELECT 1, 'One'
UNION SELECT 2, 'Two'
UNION SELECT 3, 'Three'
UNION SELECT 4, 'Four'
UNION SELECT 5, 'Five';

CREATE TABLE #odd (id INT);

CREATE NONCLUSTERED INDEX ix1
ON #odd(id);
go
WITH Num (n)
AS
(
SELECT 1
UNION ALL
SELECT n + 1
FROM Num
WHERE n < 10000
)
INSERT INTO #odd (id)
SELECT n
FROM Num
OPTION (MAXRECURSION 0)
go
SELECT *
FROM #odd
WHERE id NOT IN (
SELECT id
FROM #test
);
go
SELECT *
FROM #odd
WHERE NOT EXISTS (
SELECT id
FROM #test
WHERE #test.id = #odd.id
);
go
drop table #test;
drop table #odd;

On my computer it generates two different query plans. NOT EXISTS generates a query plan with a cost of 0.0614788. NOT IN generates a query plan with substantially higher cost of 0.121561. For small data sets in both tables the two queries generated the same query plan.

EXISTS is not just about breaking the loop, the SELECT in EXISTS uses a correlated subquery and can potentially take advantage of indexes on the table. In the best case this could result in efficient index seeks in the subquery, depending on which query plan SQL Server decides to go with.


Dave F-425609
Dave F-425609
SSC Veteran
SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)SSC Veteran (296 reputation)

Group: General Forum Members
Points: 296 Visits: 37

Wow. Tested it out and got the same results on mine. Will definitely bear that one in mind.

Thanks Mike!


Alan Nicholas
Alan Nicholas
Grasshopper
Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)Grasshopper (11 reputation)

Group: General Forum Members
Points: 11 Visits: 60

By far the best solution as it relates most closely to the problem to be solved. I also prefer to use "EXISTS" rather than "IN" as you can't use "IN" efficiently if you have a composite key.

Worth noting that you can use use " select * " rather than " select 1 " or " select 12345 " for that matter.


Mike C
Mike C
SSCrazy
SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)SSCrazy (2.8K reputation)

Group: General Forum Members
Points: 2781 Visits: 1168

Worth noting that you can use use " select * " rather than " select 1 " or " select 12345 " for that matter.

I have a sneaking suspicion that using select * or select column could result in a bookmark lookup (or RID lookup) if your query includes columns that are non-clustered index columns or non-indexed columns (particularly in the WHERE clause or in joins). Select 1 or select 12345 shouldn't encounter that problem.


Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search