MAX函数减慢查询

时间:2019-04-10 13:00:50

标签: sql sql-server

MAX函数减慢查询速度。将结果恢复正常需要花费数小时,但是当我运行某个项目时,它需要5秒钟。我正在尝试运行160万条记录。下面是代码:

RDBMS:SQL服务器

  `SELECT
   A.[GTIN]
  ,A.[Status]
      ,max(A.[Last_Changed_On]) as Last_Changed_On_OLD
  ,max(B.LastChangedOn) as Last_Changed_on_NEW
  ,(Case when (b.LastChangedOn is null) then Null else A.[Status] end) as 
    New_Status
  ,(Case when (b.LastChangedOn is not null) then Null else A.[Status] 
 end) as Old_Status

 FROM [PIMIDQ].[dbo].[Workflow_Status] A

 left Join 
              ( Select GTIN, 
              max(last_changed_on) as [LastChangedOn],
             [Status]
             from Workflow_Status
             group by
              GTIN,
              [Status]
               )  B  
              on  (A.Last_Changed_On = (B.LastChangedOn) and A.[Status] = 
B.[Status])
group by 
 A.[GTIN]
,A.[Status]
,(Case when (b.LastChangedOn is null) then Null else A.[Status] end)
,(Case when (b.LastChangedOn is not null) then Null else A.[Status] end)`

0 个答案:

没有答案