Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

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

sql - how to best organize the Inner Joins in (select) statement

let's say i have three tables, each one relates to another,

when i need to get a column from each table, does it make difference how to organize the (inner joins)??

Select table1.column1,table2.column2,table3.column2
From table1 
Inner Join table2 on ..... etc
Inner Join table3 on .....

in another words, can i put (table2) after (From )??

Select table1.column1,table2.column2,table3.column2
From table2
Inner Join table1 on ..... etc
Inner Join table3 on .....
See Question&Answers more detail:os

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

1 Answer

0 votes
by (71.8m points)

For most queries, order does not matter.

  • An INNER JOIN is both associative and commutative so table order does not matter
  • SQL is declarative. That is, how you define the query is not how the optimiser works it out. It does not do it line by line as you wrote it.

That said...

  • OUTER JOINs are neither associative nor commutative
  • For complex queries, the optimiser will "best guess" rather than go through all possibilities which "costs" too much. Table order may matter here

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...