Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

replace IN with EXIST

Posted on 2013-06-18
2
Medium Priority
?
269 Views
Last Modified: 2013-06-19
hi experts

Can theperformance of this sql be improved by using a EXIST clause instead of  IN ?

DELETE FROM CUSTOMER  WHERE ID IN (SELECT ID FROM CUSTOMER WHERE  System='MAC' AND BusinessDate='2013-05-06' )

thanks
0
Comment
Question by:royjayd
[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
2 Comments
 
LVL 16

Accepted Solution

by:
Surendra Nath earned 1200 total points
ID: 39258028
yes, it can be, if you are looking for a exists solution it can be written as below

DELETE FROM CUSTOMER  C
WHERE EXISTS (SELECT 1 FROM CUSTOMER C1 WHERE  C1.System='MAC' AND C1.BusinessDate='2013-05-06' and C.id = C1.ID)

Open in new window

0
 
LVL 49

Assisted Solution

by:PortletPaul
PortletPaul earned 400 total points
ID: 39258112
EXISTS will generally outperform IN() - and can be substantially better.
however the difference is marginal if the numbers involved are small

my personal "rule of thumb" regarding IN() is that you don't use it if the number of items within the brackets is an unknown quantity. i.e.

select * from whatever where fieldx IN (1,2,3) -- known quantity = OK
select * from whatever where fieldx IN (select X from Y) -- unknown quantity = Not OK

IN() is an abbreviated form of a list of OR operators and can be dreadfully slow if the number of items being evaluated is very large.

ps: Neo_jarvis and I also seem to agree on using "select 1 ...".  The inner subquery only needs to return a flag when using EXISTS, and the constant 1 does this nicely.

pps: for me, "rules of thumb" are not absolute, just guides.
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

This is basically a blog post I wrote recently. I've found that SARGability is poorly understood, and since many people don't read blogs, I figured I'd post it here as an article. SARGable is an adjective in SQL that means that an item can be fou…
In this article I will describe the Detach & Attach method as one possible migration process and I will add the extra tasks needed for an upgrade when and where is applied so it will cover all.
In response to a need for security and privacy, and to continue fostering an environment members can turn to for support, solutions, and education, Experts Exchange has created anonymous question capabilities. This new feature is available to our Pr…
We’ve all felt that sense of false security before—locking down external access to a database or component and feeling like we’ve done all we need to do to secure company data. But that feeling is fleeting. Attacks these days can happen in many w…

721 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