Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase 12345»»»

Complex joins Expand / Collapse
Author
Message
Posted Monday, December 5, 2011 9:17 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:21 AM
Points: 6,098, Visits: 8,367
Comments posted to this topic are about the item Complex joins


Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1216764
Posted Monday, December 5, 2011 9:18 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Thursday, November 6, 2014 1:00 PM
Points: 5,333, Visits: 25,277
A nice, complex question, but a darn good one.

If everything seems to be going well, you have obviously overlooked something.

Ron

Please help us, help you -before posting a question please read

Before posting a performance problem please read
Post #1216765
Posted Tuesday, December 6, 2011 1:44 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 12:36 AM
Points: 1,379, Visits: 510
A very nice question. I learned something again.
Post #1216810
Posted Tuesday, December 6, 2011 1:46 AM
Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Tuesday, July 9, 2013 11:12 PM
Points: 1,263, Visits: 1,081
Thanks for a great brain teaser--got my analytical half started this morning. Wonderful
Post #1216813
Posted Tuesday, December 6, 2011 2:21 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Monday, November 10, 2014 4:34 AM
Points: 1,234, Visits: 1,434
I had never heard of Nested Joins, so learned something today.

However my method of getting the right answer was to use the execution plans. One execution plan exactly matched the nested join. Once I had the right anser, I was able to work out why.
Post #1216837
Posted Tuesday, December 6, 2011 2:39 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:21 AM
Points: 6,098, Visits: 8,367
Thanks, all, for your kind words. It;s always good to know that my questions are appreciated.

Tom Brown (12/6/2011)
I had never heard of Nested Joins, so learned something today.

However my method of getting the right answer was to use the execution plans. One execution plan exactly matched the nested join. Once I had the right anser, I was able to work out why.

Ha! I like the creative thinking!
I actually used a few rows of sample data (making sure to cover every possibility) to make sure that I had not accidentally messed up the question. But your method is so much easier! (Though you have to beware of checking not just the graphical representation of the plan, but also the "hidden" extra characteristics in the properties of the operators).

My motto is that you don't have to know everything, as long as you knnow where.how to find out - and you show that there are often even multiple answers to THAT question!



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1216842
Posted Tuesday, December 6, 2011 2:47 AM


SSCrazy

SSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazySSCrazy

Group: General Forum Members
Last Login: 2 days ago @ 1:28 PM
Points: 2,386, Visits: 7,611
Enjoyed that one, would've been even better on a Monday morning to get the juices flowing!

Flabbergasted that so many people are getting it incorrect. . . I assumed that understanding the logic of join operators is integral to the majority of the visitors to this site.



Not a DBA, just trying to learn

For better, quicker answers on T-SQL questions, click on the following...
http://www.sqlservercentral.com/articles/Best+Practices/61537/

For better, quicker answers on SQL Server performance related questions, click on the following...
http://www.sqlservercentral.com/articles/SQLServerCentral/66909/



If you litter your database queries with nolock query hints, are you aware of the side effects?
Try reading a few of these links...

(*) Missing rows with nolock
(*) Allocation order scans with nolock
(*) Consistency issues with nolock
(*) Transient Corruption Errors in SQL Server error log caused by nolock
(*) Dirty reads, read errors, reading rows twice and missing rows with nolock


LinkedIn | Blog coming soon (for sufficiently large values of "soon" )!
Post #1216843
Posted Tuesday, December 6, 2011 3:18 AM


SSCommitted

SSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommittedSSCommitted

Group: General Forum Members
Last Login: Today @ 2:12 AM
Points: 1,785, Visits: 1,173
A good question - I tried to deduce it without running the code. Mmm. I knew there was a reason I never use that nested syntax. Perhaps becuase a) I didn't understand it (I do a little better now) and b) it adds nothing to the art of querying.

However I do understand that caution is in order if I need to decode such now!
Post #1216862
Posted Tuesday, December 6, 2011 3:40 AM


Ten Centuries

Ten CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen CenturiesTen Centuries

Group: General Forum Members
Last Login: Yesterday @ 5:23 AM
Points: 1,409, Visits: 1,316
Hi!
I don't understand how the condition
AND  o.OrderDate    >  DATEADD(month, -4, CURRENT_TIMESTAMP)

in SQL Query #4 works. For me it seems it is to eliminate the employees with no appropriate order attached.
Have I overlooked something?

To have 100% same behaviour, I use:
SELECT     e.Name AS SalesRep, c.Name AS Customer, o.OrderDate
FROM dbo.Customers AS c
INNER JOIN dbo.Orders AS o
ON c.CustomerID = o.CustomerID
AND o.OrderDate > DATEADD(month, -4, CURRENT_TIMESTAMP)
RIGHT JOIN dbo.Employees AS e
ON e.EmployeeID = c.SalesRep
WHERE e.Position = 'SalesRep';





See, understand, learn, try, use efficient
© Dr.Plch
Post #1216874
Posted Tuesday, December 6, 2011 4:01 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: Yesterday @ 5:21 AM
Points: 6,098, Visits: 8,367
honza.mf (12/6/2011)
Hi!
I don't understand how the condition
AND  o.OrderDate    >  DATEADD(month, -4, CURRENT_TIMESTAMP)

in SQL Query #4 works. For me it seems it is to eliminate the employees with no appropriate order attached.
Have I overlooked something?

To have 100% same behaviour, I use:
SELECT     e.Name AS SalesRep, c.Name AS Customer, o.OrderDate
FROM dbo.Customers AS c
INNER JOIN dbo.Orders AS o
ON c.CustomerID = o.CustomerID
AND o.OrderDate > DATEADD(month, -4, CURRENT_TIMESTAMP)
RIGHT JOIN dbo.Employees AS e
ON e.EmployeeID = c.SalesRep
WHERE e.Position = 'SalesRep';



Hi Honza.mf,

In query #4, the first step is the inner join between orders and customers. So for the right outer join, the left-hand side is a row set consisting of orders with their customers (where both orders without customer and customers without orders are already filtered out). On the right-hand side is the unfiltered table of employees.
The join will attempt to match each row on the left-hand side with each row on the right-hand side. It will retain combinations where the condition is true (the employee on the right-hand side is the sales rep for the order, and the order is no older than four months), and it will also retain rows from the right-hand side (employees) where the condition is not true for any of the rows of the left-hand side.

Your query filters out old orders before the outer joins even starts. I compared it to the #4 query, by logical comparison, running against my test data, and comparing execution plans. I did not find any differences, so it is also a correct rewrite of the original query - and one that is easier to comprehend! Kudos!

That being said, I have run into sitiuations where I needed to choose between the join pattern of the original query (with the nested joins), or that of query #4 (with the weird placement of the ON condition). I am 99% sure that a rewrite such as yours was not possible in that case. My goal was to reconstruct such a case for this question, and I obviously did not completely succeed at that. Fortunately, this does not invalidate the QotD (as the question is to identify the query that is exactly equivalent, not to produce the best rewrite).



Hugo Kornelis, SQL Server MVP
Visit my SQL Server blog: http://sqlblog.com/blogs/hugo_kornelis
Post #1216882
« Prev Topic | Next Topic »

Add to briefcase 12345»»»

Permissions Expand / Collapse