有什么方法可以加速这个CTE?

时间:2012-10-17 07:31:51

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

此查询需要5秒钟:

With cte as 
(
    Select  ROW_NUMBER() OVER (Order By dbo.GetLatestStatusDateTime(d.DocumentID)) peta_rn, d.DocumentID, d.IsReEfiled, d.IGroupID, d.ITypeID, d.RecordingDateTime,
        d.IDate, d.InstrumentID, d.DocumentStatusID, u.Username, j.JDAbbreviation, inf.DocumentName,  dbo.GetLatestStatusDateTime(d.DocumentID) as LatestStatusDatetime,
        it.Abbreviation as ITypeAbbreviation, d.DocumentDate, ds.Abbreviation as DocumentStatusAbbreviation 
                From Documents d Left Join IGroupes ig On d.IGroupID = ig.IGroupID 
                Left Join ITypes it On d.ITypeID = it.ITypeID 
                Left Join Users u On u.UserID = d.UserID 
                Left Join DocumentStatuses ds On d.DocumentStatusID = ds.DocumentStatusID 
                Left Join InstrumentFiles inf On d.DocumentID = inf.DocumentID 
                Left Join Jurisdictions j on j.JurisdictionID = d.JurisdictionID Where 1 = 1  And d.JurisdictionID = 1
)
Select  * from cte
                Where 1 = 1 
                And peta_rn>92000 AND peta_rn<=92100

虽然这是一个静态查询,但在Order ByRow_Number()将基于用户选择的内容时,它将是动态的。例如。目前它是dbo.GetLatestStatusDateTime(d.DocumentID)但它也可以是其他任何东西。因此,将使用if和then在代码中生成此查询。无论如何,你能在我的查询中发现任何错误吗?为什么仅仅需要5秒才能获得100条记录?我已经看到SQL服务器扩展到数百万条记录,这只是百万分之十,而且我遇到了瓶颈:(

编辑:大量改进

将查询更改为:

With cte as 
(

        Select  ROW_NUMBER() OVER (Order By d.DocumentID) peta_rn, d.DocumentID
                From Documents d Left Join IGroupes ig On d.IGroupID = ig.IGroupID 
                Left Join ITypes it On d.ITypeID = it.ITypeID 
                Left Join Users u On u.UserID = d.UserID 
                Left Join DocumentStatuses ds On d.DocumentStatusID = ds.DocumentStatusID 
                Left Join InstrumentFiles inf On d.DocumentID = inf.DocumentID 
                Left Join Jurisdictions j on j.JurisdictionID = d.JurisdictionID Where 1 = 1  And d.JurisdictionID = 1
)
Select  d.DocumentID, d.IsReEfiled, d.IGroupID, d.ITypeID, d.RecordingDateTime,
        d.IDate, d.InstrumentID, d.DocumentStatusID, u.Username, j.JDAbbreviation, inf.DocumentName, 
        it.Abbreviation as ITypeAbbreviation, d.DocumentDate, ds.Abbreviation as DocumentStatusAbbreviation 
                From Documents d Left Join IGroupes ig On d.IGroupID = ig.IGroupID 
                Left Join ITypes it On d.ITypeID = it.ITypeID 
                Left Join Users u On u.UserID = d.UserID 
                Left Join DocumentStatuses ds On d.DocumentStatusID = ds.DocumentStatusID 
                Left Join InstrumentFiles inf On d.DocumentID = inf.DocumentID 
                Left Join Jurisdictions j on j.JurisdictionID = d.JurisdictionID
                Inner Join cte On cte.DocumentID = d.DocumentID
                And peta_rn>92000 AND peta_rn<=92100

在1秒内执行查询。但我仍觉得1秒对于100条记录来说太大了。还有其他任何优化吗?

1 个答案:

答案 0 :(得分:2)

试试这个。在不知道表关系而无法测试它的情况下编辑很多代码。我希望这会有所帮助。

With d as 
(
  Select ROW_NUMBER() OVER (Order By d.DocumentID) peta_rn, 
  DocumentID, IsReEfiled, IGroupID, ITypeID, RecordingDateTime,
  IDate, InstrumentID, DocumentStatusID, JurisdictionID, UserID
  from Documents
)
Select  d.DocumentID, d.IsReEfiled, d.IGroupID, d.ITypeID, d.RecordingDateTime,
d.IDate, d.InstrumentID, d.DocumentStatusID, u.Username, j.JDAbbreviation, inf.DocumentName, 
it.Abbreviation as ITypeAbbreviation, d.DocumentDate, ds.Abbreviation as DocumentStatusAbbreviation 
From d Left Join IGroupes ig On d.IGroupID = ig.IGroupID 
Left Join ITypes it On d.ITypeID = it.ITypeID 
Left Join Users u On u.UserID = d.UserID 
Left Join DocumentStatuses ds On d.DocumentStatusID = ds.DocumentStatusID 
Left Join InstrumentFiles inf On d.DocumentID = inf.DocumentID 
Left Join Jurisdictions j on j.JurisdictionID = d.JurisdictionID
where
d.peta_rn>92000 AND d.peta_rn<=92100