Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 SQL Server 2005 Forums
 Transact-SQL (2005)
 EXISTS vs IN

Author  Topic 

Harry C
Posting Yak Master

148 Posts

Posted - 2009-08-20 : 13:04:03
Can someone help me rewrite this using EXISTS

FROM MyView
WHERE aid in (SELECT aid FROM dbo.GetList(@UserID))

The IN statement works fine, but takes a very long time. I am hoping that exists will provide some increased benefit, but cannot figure out the query. Thanks

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-08-20 : 13:08:44
Yes - IN (...) is very slow if there are many rows to read.

FROM MyView mv
WHERE EXISTS (select * from dbo.GetList(@UserID) where aid = mv.aid)

But maybe this is also slow because using functions is often slowing down the queries.

Test it.


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page

Harry C
Posting Yak Master

148 Posts

Posted - 2009-08-20 : 13:13:30
Thanks, I just figured it out and was testing it. It is way faster. 43 seconds for 470 records with IN vs 9 seconds with EXISTS. Much better. I am recoding a legacy system in .NET and trying to use the existing SQL as much as I can. But it may be better to pass in a Comma List of aid vs getting them from the the function. We'll see. Thanks!

HC
Go to Top of Page

webfred
Master Smack Fu Yak Hacker

8781 Posts

Posted - 2009-08-20 : 13:18:30
welcome


No, you're never too old to Yak'n'Roll if you're too young to die.
Go to Top of Page
   

- Advertisement -