?
Solved

SQL subquery's impact on overall performance

Posted on 2014-03-05
4
Medium Priority
?
440 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
4 Comments
 
LVL 66

Accepted Solution

by:
Jim Horn earned 668 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 70

Assisted Solution

by:Scott Pletcher
Scott Pletcher earned 668 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 664 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

It is possible to export the data of a SQL Table in SSMS and generate INSERT statements. It's neatly tucked away in the generate scripts option of a database.
This month, Experts Exchange sat down with resident SQL expert, Jim Horn, for an in-depth look into the makings of a successful career in SQL.
Familiarize people with the process of retrieving data from SQL Server using an Access pass-thru query. Microsoft Access is a very powerful client/server development tool. One of the ways that you can retrieve data from a SQL Server is by using a pa…
Viewers will learn how to use the UPDATE and DELETE statements to change or remove existing data from their tables. Make a table: Update a specific column given a specific row using the UPDATE statement: Remove a set of values using the DELETE s…

649 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