简体   繁体   中英

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?

Which block of code will get executed first (or) whether outermost block or inner most block of code in 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. A SQL query represents the result set, not the specific actions to generate it.

SQL basically has three steps in processing a query:

  • 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. You can influence the execution plan by providing hints in the query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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