|
|
|
Hall of Fame
       
Group: General Forum Members
Last Login: Thursday, May 02, 2013 3:45 PM
Points: 3,748,
Visits: 928
|
|
Really nice question. Thanks, Ron.
Hugo, thank you for the additional explanation, too. I would have not been able to understand it just by reading BOL.
"El" Jerry.
"El" Jerry.
"A watt of Ottawa" - Gerardo Galvan
|
|
|
|
|
SSCrazy
      
Group: General Forum Members
Last Login: 2 days ago @ 10:59 AM
Points: 2,579,
Visits: 1,537
|
|
| Great question and dialogue. Thanks!
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 11:54 AM
Points: 7,112,
Visits: 7,188
|
|
Nice tidy question, that brings up two importan fundamentals: (1) intersection returns distinct values not duplicates (2) equality tests do implicit conversion where needed, from lower precedence type to higher.
Tom Is minic a gheibheann béal oscailte dorn dúnta. Is minig a cheapas beul fosgailte dòrn dùinte.
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 9:43 AM
Points: 469,
Visits: 193
|
|
Hugo,
Thanks for the further explanation that supports the correct answer.
|
|
|
|
|
SSC-Addicted
      
Group: General Forum Members
Last Login: Monday, May 20, 2013 9:43 AM
Points: 469,
Visits: 193
|
|
Nice question on the topic.
Thanks.
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Friday, November 09, 2012 7:25 AM
Points: 298,
Visits: 107
|
|
Nice question, thanks.
Prior to answering I would have done something like
SELECT DISTINCT x AS 'Intersect Chars with BIGINT' FROM #A INNER JOIN #B on #b.M = #a.x
A quick execution plan shows that the INTERSECT is ever so marginally more efficient too (in this case anyway), primarily because the Nested Loop step is a Left Semi Join, where the INNER JOIN has a slightly higher nested loop step cost.
Every day's a school day!
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 3:49 PM
Points: 5,244,
Visits: 7,063
|
|
chriscoates (6/26/2012)
Nice question, thanks. Prior to answering I would have done something like SELECT DISTINCT x AS 'Intersect Chars with BIGINT' FROM #A INNER JOIN #B on #b.M = #a.x
A quick execution plan shows that the INTERSECT is ever so marginally more efficient too (in this case anyway), primarily because the Nested Loop step is a Left Semi Join, where the INNER JOIN has a slightly higher nested loop step cost. Every day's a school day! Note that they are only equivalent when the columns #a.x and #b.M are unique. If there is a primary key, unique constraint, or unique index involved, you can be sure that they are unique; otherwise you can't, so you can't just replace one with the other.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Friday, November 09, 2012 7:25 AM
Points: 298,
Visits: 107
|
|
Perhaps we are at cross purposes Hugo, I meant tht the result set is the same, regardless of uniqueness. Of course if the values are not unique then there's a man-to-many relationship that I'm sure would make my query cost soar to determine the DISTINCT results, but the output would surely be the same?
Am I missing something else obvious here?
|
|
|
|
|
SSCertifiable
       
Group: General Forum Members
Last Login: Yesterday @ 3:49 PM
Points: 5,244,
Visits: 7,063
|
|
chriscoates (6/27/2012) Perhaps we are at cross purposes Hugo, I meant tht the result set is the same, regardless of uniqueness. Of course if the values are not unique then there's a man-to-many relationship that I'm sure would make my query cost soar to determine the DISTINCT results, but the output would surely be the same?
Am I missing something else obvious here? When comparing two queries for performance, the first thing you must consider is whether they are equivalent. Not just for one set of data, but for each possible set of data. In the case of this question, the queries happen to produce the same result for the given data, but will give different results with other data, so they are not equivalent, and you cannot replace one with the other in a production system. If you add constraints to ensure that they are equivalent, the optimizer might (I didn't test this, and have no time for it either) well produce identical plans for the two queries. Without those constraints, it can't produce identical plans, since (depending on the data) it might have to return different results.
Hugo Kornelis, SQL Server MVP Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
|
|
|
|
|
SSC Veteran
      
Group: General Forum Members
Last Login: Friday, November 09, 2012 7:25 AM
Points: 298,
Visits: 107
|
|
To quote this question, and the article it refers to:
INERSECT returns "those distinct values that are common between tables"
I have never seen a situation where:
SELECT DISTINCT a.x FROM a INNER JOIN b on a.x=b.m
would not return "those distinct values that are common between tables" either, regardless of the uniqueness of a.x or b.m in either query.
I agree that the execution may be completely different in either case.
Chris
|
|
|
|