February 26, 2014 at 8:07 am
Thanks for the reply, Toreador.
February 26, 2014 at 8:11 am
RLilj33 (2/26/2014)
Equivalent to:SELECT X AS 'Intersecting'
FROM A
JOIN b ON b.y = a.x;
Except the ordering. INTERSECT returns the values in numeric order; JOIN returns the values in the order in which they were found in the left table (A).
I didn't see anything in the BOL documentation on this. Anyone shed some light on this? Thanks.
And the performance is better using INTERSECT, even without adding an ORDER BY to your statement (to get identical record sets).
Can someone explain the 'why' of the performance difference?
February 26, 2014 at 8:14 am
Equivalent to:
SELECT X AS 'Intersecting'
FROM A
JOIN b ON b.y = a.x;
It's not equivalent, INTERSECT returns DISTINCT values.
SELECT DISTINCT X AS 'Intersecting'
FROM A
JOIN b ON b.y = a.x;
February 26, 2014 at 8:36 am
Curious: the winner is?
"EXISTS" with absolute less reads.
SET STATISTICS IO ON
GO
SELECT DISTINCT x AS 'EXISTS'
FROM A
WHERE exists(SELECT * FROM B WHERE Y=x )
GO
SELECT x AS 'Intersecting'
FROM A
INTERSECT
SELECT Y
FROM B
go
SELECT DISTINCT x AS 'JOIN'
FROM A
JOIN B
ON Y=x
go
Result:
EXISTS
1
2
20
(3 row(s) affected)
Tabella 'B'. Conteggio analisi 1, letture logiche 1, letture fisiche 0, letture read-ahead 0, letture logiche LOB 0, letture fisiche LOB 0, letture read-ahead LOB 0.
Tabella 'A'. Conteggio analisi 1, letture logiche 1, letture fisiche 0, letture read-ahead 0, letture logiche LOB 0, letture fisiche LOB 0, letture read-ahead LOB 0.
Intersecting
1
2
20
(3 row(s) affected)
Tabella 'B'. Conteggio analisi 1, letture logiche 5, letture fisiche 0, letture read-ahead 0, letture logiche LOB 0, letture fisiche LOB 0, letture read-ahead LOB 0.
Tabella 'A'. Conteggio analisi 1, letture logiche 1, letture fisiche 0, letture read-ahead 0, letture logiche LOB 0, letture fisiche LOB 0, letture read-ahead LOB 0.
JOIN
1
2
20
(3 row(s) affected)
Tabella 'Worktable'. Conteggio analisi 3, letture logiche 160, letture fisiche 0, letture read-ahead 0, letture logiche LOB 0, letture fisiche LOB 0, letture read-ahead LOB 0.
Tabella 'B'. Conteggio analisi 1, letture logiche 1, letture fisiche 0, letture read-ahead 0, letture logiche LOB 0, letture fisiche LOB 0, letture read-ahead LOB 0.
Tabella 'A'. Conteggio analisi 1, letture logiche 1, letture fisiche 0, letture read-ahead 0, letture logiche LOB 0, letture fisiche LOB 0, letture read-ahead LOB 0.
February 26, 2014 at 9:06 am
easy one thanks..
February 26, 2014 at 9:17 am
I always use Exists and Not Exists instead of Intersect and Except. I have never seen Intersect used in any DB that I have worked on.
February 26, 2014 at 10:59 am
Nice and easy - thanks, Ron!
February 27, 2014 at 1:02 am
Easy one, Thanks Ron.
---------------------------------------------------
"Thare are only 10 types of people in the world:
Those who understand binary, and those who don't."
February 27, 2014 at 12:26 pm
Nice to recollect INTERSECT. Basic, but I've almost forgotten to use it. Thanks Ron!
March 3, 2014 at 2:41 am
Carlo Romagnano (2/26/2014)
First time I see 99% OK.😀
Now it`s 95% still one of the easiest QotD ever.
Thanks & Best Regards,
Hany Helmy
SQL Server Database Consultant
March 3, 2014 at 3:07 am
paul.knibbs (2/26/2014)
I had to go and run this one because I couldn't believe the answer was as simple as it first appeared. Lo and behold, it *was* that simple! :laugh:
I thought there was some trick; but it is that simple 🙂
Thanks & Best Regards,
Hany Helmy
SQL Server Database Consultant
March 6, 2014 at 11:59 am
Good question.
Thanks,
March 28, 2014 at 7:51 am
not completely 100% of right answer
April 16, 2014 at 1:02 am
Pretty straightforward. 🙂
Viewing 15 posts - 16 through 30 (of 30 total)
You must be logged in to reply to this topic. Login to reply