September 16, 2012 at 11:22 pm
I want to know which query is more optimized?
SELECT 1 FROM ABC WHERE Id IN (SELECT Id FROM ABC1)
===================================================
SELECT 1 FROM ABC a JOIN ABC1 b ON a.Id = b.Id
Thanks
Puru
September 17, 2012 at 12:18 am
purushottam2 (9/16/2012)
I want to know which query is more optimized?SELECT 1 FROM ABC WHERE Id IN (SELECT Id FROM ABC1)
===================================================
SELECT 1 FROM ABC a JOIN ABC1 b ON a.Id = b.Id
Thanks
Puru
Actually depends upon the number of records in the table.
--rhythmk
------------------------------------------------------------------
To post your question use below link
https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help
๐
September 17, 2012 at 2:01 am
Its also depends on whether the columns you are joining on are indexed.
Indexing and performance is a large topic in it's own right.
Too many indexes on a large table may slow down INSERT, UPDATE and DELETE whilst giving improvement to SELECT.
Where possible, I prefer JOINS from a readability point of view.
:exclamation: "Be brave. Take risks. Nothing can substitute experience." :exclamation:
September 17, 2012 at 3:20 am
Robin Sasson (9/17/2012)
Its also depends on whether the columns you are joining on are indexed.Indexing and performance is a large topic in it's own right.
Too many indexes on a large table may slow down INSERT, UPDATE and DELETE whilst giving improvement to SELECT.
Where possible, I prefer JOINS from a readability point of view.
But in many places I have observed that IN beats INNER JOIN in performance.
--rhythmk
------------------------------------------------------------------
To post your question use below link
https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help
๐
September 17, 2012 at 3:59 am
Thing is, those are two different queries. The IN statement can act to filter the results of ABC, but none of the columns from ABC1 are available in the SELECT or WHERE clause of the statement using IN. The JOIN statement is combining two tables, so you'll get more data returned. So we're actually not comparing the same things here. These are not equivalent statements and result sets that can be realistically compared, one to the other.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
September 17, 2012 at 4:18 am
Agree, Suppose ABC is master tables and ABC1 is child table, in this case IN and Inner Join both will return the same data.
September 17, 2012 at 4:46 am
An inner join between two tables does a complete join, it checks for matches and returns rows. This means, if there are multiple matching rows in the second table, multiple rows will be returned. Also, when two tables are joined, columns can be returned from either.
With an IN, what is done is a semi-join, a join that checks for matches but does not return rows. This means if there are multiple matching tables in the resultset used for the IN, it doesnโt matter. Only one row from the first table will be returned. Also, because the rows are not returned, columns from the table referenced in the IN cannot be returned.
Thanks!
September 17, 2012 at 4:56 am
Grant Fritchey (9/17/2012)
Thing is, those are two different queries. The IN statement can act to filter the results of ABC, but none of the columns from ABC1 are available in the SELECT or WHERE clause of the statement using IN. The JOIN statement is combining two tables, so you'll get more data returned. So we're actually not comparing the same things here. These are not equivalent statements and result sets that can be realistically compared, one to the other.
Agreed...but OP is asking about the scenario where both IN and INNER JOIN returns same result.
--rhythmk
------------------------------------------------------------------
To post your question use below link
https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help
๐
September 17, 2012 at 4:58 am
Just found below article from Gail on the same ๐
http://sqlinthewild.co.za/index.php/2010/01/12/in-vs-inner-join/
--rhythmk
------------------------------------------------------------------
To post your question use below link
https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help
๐
September 17, 2012 at 5:58 am
yuvipoy (9/17/2012)
An inner join between two tables does a complete join, it checks for matches and returns rows. This means, if there are multiple matching rows in the second table, multiple rows will be returned. Also, when two tables are joined, columns can be returned from either.With an IN, what is done is a semi-join, a join that checks for matches but does not return rows. This means if there are multiple matching tables in the resultset used for the IN, it doesnโt matter. Only one row from the first table will be returned. Also, because the rows are not returned, columns from the table referenced in the IN cannot be returned.
Thanks!
Copied from Gail's blog. Not exactly the right thing to do unless you also provide the link. It smacks of plagiarism and is most definitely frowned on around here.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
September 17, 2012 at 5:58 am
rhythmk (9/17/2012)
Grant Fritchey (9/17/2012)
Thing is, those are two different queries. The IN statement can act to filter the results of ABC, but none of the columns from ABC1 are available in the SELECT or WHERE clause of the statement using IN. The JOIN statement is combining two tables, so you'll get more data returned. So we're actually not comparing the same things here. These are not equivalent statements and result sets that can be realistically compared, one to the other.Agreed...but OP is asking about the scenario where both IN and INNER JOIN returns same result.
He didn't say that. It just says, which is optimized.
"The credit belongs to the man who is actually in the arena, whose face is marred by dust and sweat and blood"
- Theodore Roosevelt
Author of:
SQL Server Execution Plans
SQL Server Query Performance Tuning
September 17, 2012 at 6:13 am
Grant Fritchey (9/17/2012)
yuvipoy (9/17/2012)
An inner join between two tables does a complete join, it checks for matches and returns rows. This means, if there are multiple matching rows in the second table, multiple rows will be returned. Also, when two tables are joined, columns can be returned from either.With an IN, what is done is a semi-join, a join that checks for matches but does not return rows. This means if there are multiple matching tables in the resultset used for the IN, it doesnโt matter. Only one row from the first table will be returned. Also, because the rows are not returned, columns from the table referenced in the IN cannot be returned.
Thanks!
Copied from Gail's blog. Not exactly the right thing to do unless you also provide the link.
I thought that looked familiar... :hehe:
yuvipoy, there's a very clear copyright policy on my blog, I'd appreciate if it was followed in the future.
Gail Shaw
Microsoft Certified Master: SQL Server, MVP, M.Sc (Comp Sci)
SQL In The Wild: Discussions on DB performance with occasional diversions into recoverability
September 17, 2012 at 9:31 pm
Grant Fritchey (9/17/2012)
rhythmk (9/17/2012)
Grant Fritchey (9/17/2012)
Thing is, those are two different queries. The IN statement can act to filter the results of ABC, but none of the columns from ABC1 are available in the SELECT or WHERE clause of the statement using IN. The JOIN statement is combining two tables, so you'll get more data returned. So we're actually not comparing the same things here. These are not equivalent statements and result sets that can be realistically compared, one to the other.Agreed...but OP is asking about the scenario where both IN and INNER JOIN returns same result.
He didn't say that. It just says, which is optimized.
Hi Grant,
Please have a look on the scenario mentioned by OP in subsequent post
Agree, Suppose ABC is master tables and ABC1 is child table, in this case IN and Inner Join both will return the same data.
--rhythmk
------------------------------------------------------------------
To post your question use below link
https://www.sqlservercentral.com/articles/forum-etiquette-how-to-post-datacode-on-a-forum-to-get-the-best-help
๐
September 20, 2012 at 11:59 am
Lots of folks have posted informative replies; but at the end of the day, the only way to know for sure in your own situation is to gather the data.
set statistics time on
set statistics io on
Then run each query and compare the data. Also get the execution plan.
September 20, 2012 at 12:06 pm
2ndHelping (9/20/2012)
Lots of folks have posted informative replies; but at the end of the day, the only way to know for sure in your own situation is to gather the data.set statistics time on
set statistics io on
Then run each query and compare the data. Also get the execution plan.
Just remember that this will have an impact on the testing, and could skew it.
Viewing 15 posts - 1 through 15 (of 16 total)
You must be logged in to reply to this topic. Login to reply