gpt4 book ai didi

SQL Server 2005 表假脱机(Lazy spool)——性能

转载 作者:行者123 更新时间:2023-12-04 17:38:44 24 4
gpt4 key购买 nike

我有一些遗留的 SQL (SP)

  declare @FactorCollectionId       int;        select  @FactorCollectionId = collectionID from dbo.collection where name = 'Factor'
declare @changeDate datetime; set @changeDate = getDate()
declare @changeTimeID int; set @changeTImeID = convert(int, convert(varchar(8), @changeDate, 112))
declare @MaxWindowID int; select @MaxWindowID = MAX(windowID) from dbo.window

select distinct @FactorCollectionId, ElementId, T.TimeID, @changeTimeId ChangeTimeID, 1 UserID, @MaxWindowID, 0 ChangeID
, null TransactionID, SystemSourceID, changeTypeID, 'R' OlapStatus, Comment, Net0 Delta0, Net0
, 1 CreatedBy, 1 UpdatedBy, @changeDate CreatedDate, @changeDate UpdatedDate, 1 CurrentRecord, MeasureTypeID
from dbo.aowCollectedFact FV
inner join dbo.timeView T on T.timeID >= FV.timeID
where FV.currentRecord = 1 --is current record
and T.CurrentHorizon <> 0 --Indicator that Time is part of current horizon
and FV.collectionID = @FactorCollectionId --factor collections only
and FV.timeID = (select MAX(timeID) --latest collected fact timeID for given collectionID and elementID
from aowCollectedFact FV2
where FV2.collectionId = @FactorCollectionId
and FV2.elementId = FV.elementID)
and (((T.ForecastLevel = 'Month') and (T.FirstDayInMonth = T.Date)) --Date is first of month for monthly customers, or
or
((T.ForecastLevel = 'Quarter')and (T.FirstDayInQuarter = T.Date))) --Date is first of quarter for quarterly customers
and not exists (select 1 --Record does not already exist in collected fact view
from aowCollectedFact FV3 -- for this factor collection, elementID, and timeID
where FV3.collectionId = @FactorCollectionId
and FV3.elementID = FV.elementId
and FV3.timeID = T.timeID)

此 SQL 处理超过 200 万行。我需要改进它的性能。当我查看执行计划时,我发现很多时间都花在了 Table Spool (Lazy spool) 上。操作(表中存在索引并且它们运行良好)。

如何提高这部分的性能?

最佳答案

在查看执行计划或表索引之前,我将给出最有根据的猜测。首先,这里有几个值得一读的链接。

showplan operator of the week - lazy spool

Table spool/Lazy spool

索引 :查看您的索引以确保它们都覆盖了您从表中选择的列。您将希望获得包含在索引中的 JOIN 和 WHERE 子句中的所有列。 SELECT 语句中的所有其他列都应该被索引包含或覆盖。

运营商 : 看看是否可以去掉不等于 ("<>") 运算符,转而使用单个大于或小于运算符。可以这样声明and T.CurrentHorizon <> 0改成这个 and T.CurrentHorizon > 0 ?

加入 : 去掉连接到自身之外的表的子查询。例如,这一行 and FV2.elementId = FV.elementID可能会导致一些问题。没有理由不能将其从子查询中移出并加入到 dbo.aowCollectedFact FV 的 JOIN 中。 ,鉴于您已经在主查询中进行了分组 (DISTINCT)。

区别 : 将其更改为 GROUP BY。我没有别的理由,因为这是一个很好的练习,需要两分钟。

最后备注 :上述所有情况的异常(exception)可能是离开最后的子查询,IF NOT EXISTS , 作为子查询。如果将其更改为 JOIN,则必须是 LEFT JOIN...WHERE NULL语句,这实际上会导致假脱机操作。没有什么好方法可以绕过那个。

关于SQL Server 2005 表假脱机(Lazy spool)——性能,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9720988/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com