0

此查询需要 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 ByinRow_Number()将基于用户选择的内容。例如。目前是,dbo.GetLatestStatusDateTime(d.DocumentID)但它也可以是其他任何东西。因此,此查询将使用 if 和 then 在代码中生成。无论如何,您能在我的查询中发现任何错误吗?为什么仅仅 100 条记录需要 5 秒?我已经看到 SQL Server 扩展到数百万条记录,这只是百万的 1/10,我正遭受瓶颈:(

编辑:大幅改进

将查询更改为:

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 条记录来说太大了。请问还有其他优化吗?

4

1 回答 1

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
于 2012-10-17T08:57:28.097 回答