Results 1 to 7 of 7

Thread: Resolved: Performance Choice: IF Exists, or use Join

  1. #1

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Resolved Resolved: Performance Choice: IF Exists, or use Join

    I have two tables. I will allow a record to be returned from one table if a bit flag is set in the other. So the question:

    Is it faster to perform an IF EXISTS query the the second table to see if a record exists where the bit is set, or is it better to use an inner join.

    This is solely about performance, and not about pure set semantics.

    Cheers,

    M

    (Using SQL 2k)
    Last edited by yrwyddfa; Jan 24th, 2007 at 10:35 AM.
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  2. #2
    Super Moderator si_the_geek's Avatar
    Join Date
    Jul 2002
    Location
    Bristol, UK
    Posts
    41,974

    Re: Performance Choice: IF Exists, or use Join

    Things like this depend on a few factors, such as the number of rows in each table and the indexes you have..

    I would expect that Exists would be slower (as you need to run a query per row), but an IN sub-query would be at roughly the same level as a Join.

    I would definitely recommend trying the options out to see which is fastest in your situation.

  3. #3
    Fanatic Member kaffenils's Avatar
    Join Date
    Apr 2004
    Location
    Norway
    Posts
    946

    Re: Performance Choice: IF Exists, or use Join

    As si_the_geek says: It depends.

    If you are running SQL Server, I would recommend that you execute both queries and display the Actual Exection Plan. This will give you a good indication on which query is faster. If you have never analyzed a execution plan it can be quite time consuming to understand in the beginning, but once you get the hang of it it will give you a lot of important information about the performance of a query.

  4. #4
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Performance Choice: IF Exists, or use Join

    As already said you can find this out yourself by looking at execution plans...

    But keep in mind, as a general rule, that a JOIN is a natural (thus fast) concept for the query optimizer and query executer to perform.

    a WHERE EXISTS IN (sub-query) is much less clear. If the sub-query has a WHERE clause that relies on a row value from the main-query it's going to be executed for each and every row's consideration - that's expensive.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  5. #5

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Performance Choice: IF Exists, or use Join

    Quote Originally Posted by szlamany
    But keep in mind, as a general rule, that a JOIN is a natural (thus fast) concept for the query optimizer and query executer to perform.

    a WHERE EXISTS IN (sub-query) is much less clear. If the sub-query has a WHERE clause that relies on a row value from the main-query it's going to be executed for each and every row's consideration - that's expensive.
    Many thanks. I've used the Query Optimiser for years, and it really doesn't show a great deal of difference on a single user DB - hence useless. I needed corroborated ideas that will enable queries to be run in a multi user scenario - and this - which I've now confirmed, is the answer I'm looking for.

    I'll keep to joins. Thanks szlamany!
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

  6. #6
    MS SQL Powerposter szlamany's Avatar
    Join Date
    Mar 2004
    Location
    Connecticut
    Posts
    18,263

    Re: Resolved: Performance Choice: IF Exists, or use Join

    Although I believe that Kaffenils suggestion was to run both queries in QUERY ANALYZER with the SHOW EXECUTION PLAN toggled to on (QUERY menu - SHOW EXECUTION PLAN).

    Then the time spent in each step of the query is displayed and can be analyzed for it's effect.

    *** Read the sticky in the DB forum about how to get your question answered quickly!! ***

    Please remember to rate posts! Rate any post you find helpful - even in old threads! Use the link to the left - "Rate this Post".

    Some Informative Links:
    [ SQL Rules to Live By ] [ Reserved SQL keywords ] [ When to use INDEX HINTS! ] [ Passing Multi-item Parameters to STORED PROCEDURES ]
    [ Solution to non-domain Windows Authentication ] [ Crazy things we do to shrink log files ] [ SQL 2005 Features ] [ Loading Pictures from DB ]

    MS MVP 2006, 2007, 2008

  7. #7

    Thread Starter
    Frenzied Member yrwyddfa's Avatar
    Join Date
    Aug 2001
    Location
    England
    Posts
    1,253

    Re: Resolved: Performance Choice: IF Exists, or use Join

    Quote Originally Posted by szlamany
    Although I believe that Kaffenils suggestion was to run both queries in QUERY ANALYZER with the SHOW EXECUTION PLAN toggled to on (QUERY menu - SHOW EXECUTION PLAN).

    Then the time spent in each step of the query is displayed and can be analyzed for it's effect.
    As I've said, I've done this for years - it really isn't that helpful in my query unless I can get 10,000 users on board (a slight exaggeration) I just couldn't weight up the options unless I had a third opinion (yours) which swayed it in the right direction.

    Thanks again.
    "As far as the laws of mathematics refer to reality, they are not certain; and as far as they are certain, they do not refer to reality." - Albert Einstein

    It's turtles! And it's all the way down

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width