Solved

SQL subquery's impact on overall performance

Posted on 2014-03-05
4
413 Views
Last Modified: 2014-03-10
What's the difference in performance of the following two queries?

Query 1
SELECT *
FROM Table1 T1 JOIN Table2 T2 ON T1.Id = T2.Id
WHERE T1.Age > 25
AND ...
AND ...
AND T1.Id NOT IN (Select T3.Id FROM Table3 T3 WHERE T3.Id = T1.Id)

Open in new window


Query 2
SELECT *
FROM Table1 T1 JOIN Table2 T2 ON T1.Id = T2.Id
WHERE T1.Age > 25
AND ...
AND ...
AND T1.Id NOT IN (Select T3.Id FROM Table3 T3)

Open in new window

0
Comment
Question by:pzozulka
4 Comments
 
LVL 65

Accepted Solution

by:
Jim Horn earned 167 total points
ID: 39908404
Query1 is a correlated subquery, where the subquery is executed ONCE FOR EACH ROW in the main query, so it will execute much slower.

Query 2 is an IN clause with a subquery that returns a simple list, and executes only once.
0
 
LVL 69

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 167 total points
ID: 39910253
The first query's condition can never be true, since t1.id is basically being NOT'd to itself,
so no rows can ever be returned, so presumably it will run quickly anyway :-).
0
 
LVL 8

Author Comment

by:pzozulka
ID: 39910444
What about query 2, is there another way to write it to increase performance. Perhaps somehow eliminating the subquery, and instead moving it to the FROM clause? If so, how would you re-write it?
0
 
LVL 9

Assisted Solution

by:DrewKjell
DrewKjell earned 166 total points
ID: 39910888
Something like this:
SELECT *
FROM Table1 T1 JOIN Table2 T2 ON T1.Id = T2.Id
LEFT OUTER JOIN Table3 T3 on T3.Id = T1.Id
WHERE T1.Age > 25
AND ...
AND ...
AND T3.ID IS NULL

Open in new window

0

Featured Post

The Eight Noble Truths of Backup and Recovery

How can IT departments tackle the challenges of a Big Data world? This white paper provides a roadmap to success and helps companies ensure that all their data is safe and secure, no matter if it resides on-premise with physical or virtual machines or in the cloud.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
I have a large data set and a SSIS package. How can I load this file in multi threading?
Using examples as well as descriptions, and references to Books Online, show the documentation available for date manipulation functions and by using a select few of these functions, show how date based data can be manipulated with these functions.
Via a live example, show how to backup a database, simulate a failure backup the tail of the database transaction log and perform the restore.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question