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

后端 未结 2 848
广开言路
广开言路 2020-12-17 23:42

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)??

相关标签:
2条回答
  • 2020-12-18 00:15

    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
    0 讨论(0)
  • 2020-12-18 00:18

    The inner join operation has left to right associativity. It doesn't matter much in which order you write the tables, as long as you don't refer to any tables in the ON condition before they have been joined.

    When the statement is executed the database engine will determine the best order to execute the join and this may be different from the order they appear in the SQL query.

    0 讨论(0)
提交回复
热议问题