SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 better performance - NOT in or Join
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

subhaoviya
Posting Yak Master

India
135 Posts

Posted - 10/09/2012 :  05:34:49  Show Profile  Reply with Quote
Hi,

I am having query

SELECT * from Table1 where ID not in
(SELECT ID from Table2)

which have million of records Table1 and thousands of records in Table2.

for executing this query it taken more than 10 min

How to fast it up?

sql-programmers
Posting Yak Master

USA
190 Posts

Posted - 10/09/2012 :  05:41:30  Show Profile  Visit sql-programmers's Homepage  Reply with Quote
Please try this and update I hope which will help you.

SELECT * from Table1
LEFT JOIN Table2 ON Table2.ID = Table1.ID
Where Table2.ID IS NULL

I think, JOIN will be better than NOT IN.

SQL Server Programmers and Consultants
http://www.sql-programmers.com/

Edited by - sql-programmers on 10/09/2012 05:43:44
Go to Top of Page

visakh16
Very Important crosS Applying yaK Herder

India
52317 Posts

Posted - 10/09/2012 :  23:12:33  Show Profile  Reply with Quote
what all indexes you've on table?

------------------------------------------------------------------------------------------------------
SQL Server MVP
http://visakhm.blogspot.com/

Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000