|
-
Jan 24th, 2007, 06:47 AM
#1
Thread Starter
Frenzied Member
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
-
Jan 24th, 2007, 09:30 AM
#2
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.
-
Jan 24th, 2007, 10:25 AM
#3
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.
-
Jan 24th, 2007, 10:29 AM
#4
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.
-
Jan 24th, 2007, 10:35 AM
#5
Thread Starter
Frenzied Member
Re: Performance Choice: IF Exists, or use Join
 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
-
Jan 24th, 2007, 10:40 AM
#6
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.
-
Jan 24th, 2007, 10:41 AM
#7
Thread Starter
Frenzied Member
Re: Resolved: Performance Choice: IF Exists, or use Join
 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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|