Sql-Server

估計執行計劃 SQL Server 排序?

  • February 20, 2022

我正在執行一個查詢,它的執行時間很長,查看執行計劃我可以看到 51% 的成本在 SORT 中?在實際查詢中,我沒有按任何東西排序。

任何人都知道如何降低此 SORT 百分比或將其全部刪除?這是我正在執行的查詢。

SELECT Time_ID,
      Site_Type_ID,
      Abandoned_ID,
      WorkType_ID,
      SUM (staging.dbo.measure.ring_time) AS Ring_Time,
      SUM (staging.dbo.measure.hold_time) AS Hold_Time,
      SUM (staging.dbo.measure.talk_time) AS Talk_Time,
      SUM (staging.dbo.measure.acw_time)  AS ACW_Time,
      COUNT(*)                            CallCount
FROM   measure
      INNER JOIN DataMartEnd.dbo.Time_Dim
        ON measure.StartTimeDate BETWEEN Time_Dim.Time_Start AND Time_Dim.Time_End
      INNER JOIN datamartend.dbo.Site_Type_Dim
        ON measure.DBID = Site_Type_Dim.Site_Type_Code
      INNER JOIN datamartend.dbo.Abandoned_Call_Dim
        ON measure.Abandoned = Abandoned_Call_Dim.abandoned_value
      INNER JOIN DataMartEnd.dbo.Work_Type_Dim
        ON measure.DBID = work_type_dim.MIG_Site_ID
           AND Work_Type_Dim.Work_Type_Code = measure.Queue
           AND measure.StartTimeDate BETWEEN Work_Type_Dim.DimEffectiveStartDtm AND Work_Type_Dim.DimEffectiveEndDtm
GROUP  BY Abandoned_ID,
         WorkType_ID,
         Site_Type_ID,
         time_id 

謝謝您的幫助。

Group by Abandoned_ID, WorkType_ID , Site_Type_ID, time_id

實現分組依據的一種方法是對輸入進行排序。Stream Aggregate Showplan Operator :

Stream Aggregate 運算符需要按其組內的列排序的輸入。如果由於先前的 Sort 運算符或由於有序索引查找或掃描而尚未對數據進行排序,則優化器將在此運算符之前使用 Sort 運算符。

QED。

進一步閱讀:關於Stream Aggregate和替代Hash Aggregate的Craig Freedman 部落格條目。

引用自:https://dba.stackexchange.com/questions/39041