为什么分拣费用我70%?

时间:2012-05-25 22:32:26

标签: sql sql-server sql-server-2008

我的观点很慢:

ALTER  view [dbo].[TAT] as
WITH d AS ( SELECT TAT = CASE WHEN TAT > 191 THEN 192 ELSE TAT END,
   [MONTH entered],
   [YEAR] = DATEPART(yyyy,[datetime entered])
  FROM [SalesDWH].[dbo].[TurnAround] TurnAround
  left outer join hermes.lom.dbo.lom_specimen Lom
  on TurnAround.[specimen id] = lom.specimen
  LEFT outer join SalesDWH.dbo.Isomers Isomers
  on TurnAround.[specimen id] = Isomers.[accession id]
  WHERE lom.specimen is null
  and isomers.[accession id] is null
 -- GROUP BY CASE WHEN TAT > 191 THEN 192 ELSE TAT END 
    --[datetime entered],[Month Entered]
),
n AS ( SELECT TOP (192) n = ROW_NUMBER() OVER (ORDER BY [object_id]) 
  FROM sys.all_columns ORDER BY [object_id]
)
SELECT TAT = n.n,

  [MONTH entered],
  [YEAR]

FROM n LEFT OUTER JOIN d 
ON n.n = d.TAT  



GO

当我查看执行计划时,它告诉我,SORTING的其中一个步骤花费了我70%。

当我检查SORTING步骤时,它表示我的ORDER BY是:

[SalesDWH].[dbo].[QuickLabDump].Specimen ID Ascending, [SalesDWH].[dbo].[QuickLabDump].Date Entered Ascending, [SalesDWH].[dbo].[QuickLabDump].Time Entered Ascending, [SalesDWH].[dbo].[QuickLabDump].Date Completed Ascending, [SalesDWH].[dbo].[QuickLabDump].Time Completed Ascending, [SalesDWH].[dbo].[QuickLabDump].Practice Name Ascending, [SalesDWH].[dbo].[QuickLabDump].Practice Code Ascending, [SalesDWH].[dbo].[QuickLabDump].Order Count Ascending, [SalesDWH].[dbo].[QLMLISMapping].MLIS Practice ID Ascending, [SalesDWH].[dbo].[adjustedtime].TimeDifference Ascending

但我没有在任何地方指定ORDER BY。

问题:

  1. 如果我没有任何ORDER BY,它为什么会说ORDER BY?
  2. 如何加快此观看速度?
  3. 我不知道是否需要此信息,但上面的视图正在查询另一个名为TURNAROUND的视图:

    USE [SalesDWH]
    GO
    
    /****** Object:  View [dbo].[TurnAround]    Script Date: 05/25/2012 15:32:08 ******/
    SET ANSI_NULLS ON
    GO
    
    SET QUOTED_IDENTIFIER ON
    GO
    
    
    
    
    ALTER VIEW [dbo].[TurnAround]
    AS
    SELECT     dbo.QuickLabDump.[Specimen ID], DATEPART(yyyy, dbo.QuickLabDump.[Date Entered]) AS [Year Entered], CAST(CAST(dbo.QuickLabDump.[Date Entered] AS DATE) 
                          AS DATETIME) + CAST(dbo.QuickLabDump.[Time Entered] AS TIME) AS [DateTime Entered], CAST(CAST(dbo.QuickLabDump.[Date Completed] AS DATE) AS DATETIME) 
                          + CAST(dbo.QuickLabDump.[Time Completed] AS TIME) AS [DateTime Completed], dbo.QuickLabDump.[Practice Code] AS [QL Practuce Code], 
                          dbo.QLMLISMapping.[MLIS Practice ID], DATEPART(m, dbo.QuickLabDump.[Date Entered]) AS [Month Entered], ROUND(CAST(DATEDIFF(mi, 
                          CAST(CAST(dbo.QuickLabDump.[Date Entered] AS DATE) AS DATETIME) + CAST(dbo.QuickLabDump.[Time Entered] AS TIME), 
                          CAST(CAST(dbo.QuickLabDump.[Date Completed] AS DATE) AS DATETIME) + CAST(dbo.QuickLabDump.[Time Completed] AS TIME)) AS FLOAT) / 60, 0) AS TAT, 
                          ROUND(CAST(DATEDIFF(mi, CAST(CAST(dbo.QuickLabDump.[Date Entered] AS DATE) AS DATETIME) + CAST('9:00AM' AS TIME), 
    
    
                          CAST(CAST(dbo.QuickLabDump.[Date Completed] AS DATE) AS DATETIME) 
                          + CAST(dbo.QuickLabDump.[Time Completed] AS TIME)) AS float) / 60, 0) 
    
                          AS [Hours TurnAround Since 9AM], TAT9AMStateAdjusted=                   
    
    
    
                          ROUND(CAST(DATEDIFF(mi, CAST(CAST(dbo.QuickLabDump.[Date Entered] AS DATE) AS DATETIME) 
                          + CAST('9:00AM' AS TIME),                                            
                          CAST(CAST(dbo.QuickLabDump.[Date Completed] AS DATE) AS DATETIME) 
                          + CAST(dbo.QuickLabDump.[Time Completed] AS TIME)) AS float) / 60, 0)                         
    
    
                          +dbo.adjustedtime.timedifference
                          ,dbo.QuickLabDump.[Order Count], CONVERT(VARCHAR(8), DATEADD(D, - (1 * DATEPART(dw, dbo.QuickLabDump.[Date Entered])) + 6,
                           dbo.QuickLabDump.[Date Entered]), 1) AS [Week Ending]
    FROM         dbo.QuickLabDump 
                 INNER JOIN dbo.QLMLISMapping 
                 ON dbo.QuickLabDump.[Practice Code] = dbo.QLMLISMapping.[Quicklab ID]
                 left outer join dbo.AccountState
                 on dbo.QuickLabDump.[Practice Code]=dbo.AccountState.[Account]
                 left outer join dbo.AdjustedTime 
                 ON dbo.AccountState.[state]=dbo.adjustedtime.[state]
    WHERE     (dbo.QuickLabDump.[Practice Code] NOT LIKE 'test%') AND (dbo.QuickLabDump.[Specimen ID] NOT LIKE 'of%') AND (dbo.QuickLabDump.Outcome <> 'rejected')
    GROUP BY dbo.QuickLabDump.[Specimen ID], dbo.QuickLabDump.[Date Entered], dbo.QuickLabDump.[Time Entered], dbo.QuickLabDump.[Date Completed], 
                          dbo.QuickLabDump.[Time Completed], dbo.QuickLabDump.[Practice Name], dbo.QuickLabDump.[Practice Code], dbo.QuickLabDump.[Order Count], 
                          dbo.QLMLISMapping.[MLIS Practice ID],dbo.adjustedtime.timedifference
    HAVING      (dbo.QuickLabDump.[Order Count] = MAX(dbo.QuickLabDump.[Order Count])) OR
                          (dbo.QuickLabDump.[Order Count] IS NULL)
    
    
    
    
    GO
    

    以下是表格定义:

    CREATE TABLE [dbo].[QuickLabDump](
        [Specimen ID] [varchar](50) NULL,
        [Client Key] [int] NULL,
        [Outcome] [varchar](50) NULL,
        [Medications] [varchar](max) NULL,
        [Date Collected] [date] NULL,
        [Time Collected] [time](0) NULL,
        [Date Entered] [date] NULL,
        [Time Entered] [time](0) NULL,
        [Date Completed] [date] NULL,
        [Time Completed] [time](0) NULL,
        [Test Date] [date] NULL,
        [Test Time] [time](0) NULL,
        [Practice Name] [varchar](500) NULL,
        [Practice Code] [varchar](50) NULL,
        [Requesting Physician] [varchar](500) NULL,
        [Other Medications] [varchar](max) NULL,
        [Order Comments] [varchar](max) NULL,
        [Reference Number] [varchar](500) NULL,
        [Order Count] [int] NULL,
        [QuickLabDumpID] [int] NOT NULL,
        [Source] [varchar](50) NULL,
        [MLIS] [varchar](50) NULL,
     CONSTRAINT [PK_QuickLabDump_1] PRIMARY KEY CLUSTERED 
    (
        [QuickLabDumpID] ASC
    )WITH (PAD_INDEX  = OFF, STATISTICS_NORECOMPUTE  = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS  = ON, ALLOW_PAGE_LOCKS  = ON) ON [PRIMARY]
    ) ON [PRIMARY]
    

4 个答案:

答案 0 :(得分:2)

您看到的列是TurnAround的GROUP BY子句中的列。执行GROUP BY的最简单方法是首先按组列排序,然后按组拆分行,最后计算每个组的聚合,在这种情况下,它看起来就像数据库正在做的那样。

答案 1 :(得分:2)

您的分组依据和(外部)联接可能是排序的原因,您在使用的列上是否有任何匹配的索引?如果您发布表的完整定义(包括索引和键/约束),那么找到问题就不那么猜测了。

答案 2 :(得分:2)

查询分析器向您显示您的Group BY正在进行排序。

您应该在group by正在使用的字段上测试\ create index以提高性能。

以下是有关Group By如何创建排序的更详细说明。 Link

答案 3 :(得分:1)

为了确保您知道,我看到了一个订单:

,
n AS ( SELECT TOP (192) n = ROW_NUMBER() OVER (ORDER BY [object_id]) 
  FROM sys.all_columns ORDER BY [object_id]
)