August 31, 2011 at 8:02 pm
Comments posted to this topic are about the item The Right Kind Of Join II[/url]
The previous question in this series is here: The Right Kind Of Join[/url]
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
August 31, 2011 at 8:33 pm
I couldn't include a graphic in the answer, so here's the query and execution plan:
DECLARE @a TABLE (a INT NOT NULL UNIQUE)
DECLARE @b-2 TABLE (b INT NOT NULL)
SELECT COUNT_BIG(*)
FROM @a
RIGHT LOOP JOIN @b-2 ON
[@B].b = [@A].a
Notice the plan does not touch table @a.
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
September 1, 2011 at 12:44 am
Nice question. Better than the question is the explanation given.
Thanks Paul.
Jason...AKA CirqueDeSQLeil
_______________________________________________
I have given a name to my pain...MCM SQL Server, MVP
SQL RNNR
Posting Performance Based Questions - Gail Shaw[/url]
Learn Extended Events
September 1, 2011 at 1:00 am
This is one of those questions where the common sense answer (e.g. both tables have no data in, so of course the answer will be zero rows!) is actually right...I knew nothing about the stuff explained in the answer, so I learned something today.
September 1, 2011 at 1:50 am
Indeed, the question and the explanation are great.
Thank you,
Iulian
September 1, 2011 at 3:01 am
Got it wrong (yes, I didn't run the code) 😀
Excellent question which shows how the query optimizer works.
A similar example is how the INNER JOIN is removed (resulting in only a scan on table "child") in the following example, due to the fact that parent_id cannot be NULL and has a FK reference to table "parent".
create table parent(id int primary key)
create table child(id int primary key, parent_id int not null references parent(id))
select c.*
from dbo.child c
inner join parent p on p.id = c.parent_id
September 1, 2011 at 3:38 am
Nils Gustav Stråbø (9/1/2011)
A similar example is how the INNER JOIN is removed (resulting in only a scan on table "child") in the following example, due to the fact that parent_id cannot be NULL and has a FK reference to table "parent".
There's an interesting limitation here: this simplification does not work if the FK relationship uses a compound key (another good reason to use surrogate keys, some would say).
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
September 1, 2011 at 3:50 am
Excellent question and explanation.
But it makes the behaviour in the first right join question seem even more bizarre than it seemed before. In this case there's a query that by very simple but not blindingly obvious reasoning can be shown to be equivalent to the original query and eliminates right loop join, and despite that lack of blinding obviousness the optimiser does the transformation and provides a plan. In the first case there's a blindingly obvious equivalent query (using left join) that doesn't run into the "no loop with right join" restriction, and the optimiser doesn't do the transformation despite the total obviousness. Not only is the statement in BoL misleading, and the actual behaviour with right loop inconsistent, but also the optimiser's choice of when to produce a plan and when not seems perverse! Or actually, it's the whole concept of "no loop with right join" that is ridiculous and perverse, because every right join is (trivially) equivalent to a left join, and there's no "no loop with left join" restriction - to make any sense at all the restriction would have to be purely syntactic, with no semantic content, but this QoTD demonstrates that it is not a syntactic restriction.
Tom
September 1, 2011 at 3:55 am
Hi Tom,
The third question in this series will be published next week (8 September).
I promise your question will be answered then.
Paul
Paul White
SQLPerformance.com
SQLkiwi blog
@SQL_Kiwi
September 1, 2011 at 5:15 am
thanks Paul!!!
Really, this question is very interesting!!
today , I learned anything!!!!
September 1, 2011 at 5:48 am
Good question, I learned something new today.
http://brittcluff.blogspot.com/
September 1, 2011 at 7:37 am
Very interesting question, thanks Paul.
September 1, 2011 at 7:59 am
paul.knibbs (9/1/2011)
This is one of those questions where the common sense answer (e.g. both tables have no data in, so of course the answer will be zero rows!) is actually right...I knew nothing about the stuff explained in the answer, so I learned something today.
Me too. Good question and answer, thanks.
September 1, 2011 at 8:38 am
Thanks Kiwi! Look forward to the next installment. 🙂
Viewing 15 posts - 1 through 15 (of 19 total)
You must be logged in to reply to this topic. Login to reply