简体   繁体   English

SQL Join 查询执行的顺序是什么?

[英]What will be the sequence of SQL Join query execution?

When we have multiple Join queries , Derived table join query and finally where conditions, What will be the sequence of SQL Join query execution?当我们有多个Join查询、Derived table join查询和最后where条件时,SQL Join查询执行的顺序是什么?

Which block of code will get executed first (or) whether outermost block or inner most block of code in SQL ?哪个代码块将首先(或)在 SQL 中是最外层的代码块还是最内层的代码块?

select 
  TablePerson.PersonName, TablePet.PetName, TablePet.PetType
from 
  TablePerson
left outer join 
(
  select TablePet.ownerID, TablePet.PetName, TableTypes.PetType
  from TablePet 
  inner join TableTypes on TablePet.PetTypeID = TableTypes.TypeID
) 
  TablePet
on
  TablePet.OwnerID = TablePerson.PersonID

SQL is a declarative language, not a procedural language. SQL 是一种声明性语言,而不是一种过程性语言。 A SQL query represents the result set, not the specific actions to generate it. SQL 查询表示结果集,而不是生成它的具体操作。

SQL basically has three steps in processing a query: SQL 在处理查询时基本上分为三个步骤:

  • Parsing the query解析查询
  • Optimizing the query to generate the best execution plan for the data优化查询以生成数据的最佳执行计划
  • Executing the query执行查询

The exact same query can have different execution plans at different times, depending on the data and the environment.完全相同的查询在不同时间可以有不同的执行计划,具体取决于数据和环境。

This is very important to understand.理解这一点非常重要。 You cannot specify what the execution plan is.您无法指定执行计划是什么。 The optimizer determines that -- regardless of CTEs and subqueries.优化器确定——不管 CTE 和子查询。 You can influence the execution plan by providing hints in the query.您可以通过在查询中提供提示来影响执行计划。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM