Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
442 views
in Technique[技术] by (71.8m points)

sql - Odd INNER JOIN syntax and encapsulation

I'm usually pretty well versed with JOINS, but this is new.

Suppose three tables (classic case of two tables and a third, linker table):

Customer  Product  Transaction
--------  -------  -----------
ID        ID       CustomerID
Name      Desc     ProductID
          Cost     Date

(Simplistic on purpose, I can't reproduce the actual structure, it's not my code.)

Normally, to get a table of "who bought what when", I'd do this:

SELECT Customer.Name, Product.Desc, Transaction.Date
FROM Product
INNER JOIN Transaction ON Transaction.ProductID = Product.ID
INNER JOIN Customer ON Transaction.CustomerID = Customer.ID

But I've been presented with this:

SELECT Customer.Name, Product.Desc, Transaction.Date
FROM Product
INNER JOIN ( Transaction
      INNER JOIN Customer ON Transaction.CustomerID = Customer.ID)
ON Transaction.ProductID = Product.ID

What's this? Just another syntax, or a performance trick?

(It's on SQLServer, FYI, but presumably that could be applied to others...)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

The parentheses do not change the semantics. The position of the ON clause controls the order of the logical processing of joins.

First Query

SELECT Customer.Name,
       Product.Desc,
       Transaction.Date
FROM   Product
       INNER JOIN Transaction
         ON Transaction.ProductID = Product.ID
       INNER JOIN Customer
         ON Transaction.CustomerID = Customer.ID 

Second Query

(Redundant parentheses removed)

SELECT Customer.Name,
       Product.Desc,
       Transaction.Date
FROM   Product
       INNER JOIN Transaction
                  INNER JOIN Customer
                    ON Transaction.CustomerID = Customer.ID
         ON Transaction.ProductID = Product.ID 

So logically in your first example the join on Transaction, Product happens first then the virtual table resulting from that is joined onto Customer, whereas in your second example the join on Transaction, Customer happens first then the virtual table resulting from that is joined on to Product

This is only logically and as inner joins are both associative and commutative this likely won't make any difference to the execution plan (unless you add OPTION (FORCE ORDER) to the query) but it can do for outer joins.

This is covered by Itzik Ben Gan here but the article has a number of inaccuracies, see the follow up letter by Lubor Kollar as well.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...