复杂查询中的执行顺序

1 sql-server t-sql

我知道简单查询的查询执行顺序。

但是,我对理解此查询中的确切执行顺序感到困惑:

select 
      M.DateKey  As DateKey ,
      PersonnelCode As PersonnelCode,
      Isnull((select top 1 BranchCode 
                  from [Financial].[viwPersonnelLocation]
              where PersonnelCode = p.PersonnelCode and DateKey <= M.DateKey 
                  order by DateKey desc)
                  ,p.BranchCode) As BranchCode,

          Isnull((select top 1 CertficateCode
                   from [Financial].[viwPersonnelEducation]
                   where PersonnelCode = p.PersonnelCode and DateKey <= M.DateKey 
                   order by DateKey desc)
                   ,p.Educationcode) As Educationcode

    from (select DateKey,MonthOfYearKey,EDATE 
          from [Common].[DimTime] 
          where LastDayOfMonthKey = 1) M

                Inner Join 

               [Financial].[viwPersonnelInfo] As P  
                   on m.DateKey >= p.DateOfEmployeement
    where (m.DateKey <= p.DateOfLeave) or (p.DateOfLeave='' or p.DateOfLeave is null) and m.EDATE <= getdate()
Run Code Online (Sandbox Code Playgroud)

Rob*_*ley 7

您应该查看执行计划以了解确切顺序。由于计划依赖于基于索引、统计数据等的启发式方法,因此您不能简单地从查询中分辨出来。

所以在 SSMS 中按 Ctrl-L 并查看执行计划。每个操作员调用其右侧的操作员,根据需要请求行。

执行计划中的物理顺序可能与查询中表达的逻辑顺序有很大不同。Itzik Ben-Gan 有一个很好的流程图