我的数据库中有大约 90k 条记录,我运行以下查询:
这需要太多时间(大约 4 秒):
SELECT * FROM (SELECT
ROW_NUMBER() OVER (Order By DocumentID desc) peta_rn, peta_query.* From
(
Select d.DocumentID, d.IsReEfiled, d.IGroupID, d.ITypeID, d.RecordingDateTime, dbo.GetLatestStatusDateTime(d.DocumentID) as LatestStatusDatetime,
dbo.FnCanChangeDocumentStatus(d.DocumentStatusID,d.DocumentID) as CanChangeStatus, d.IDate, d.InstrumentID, d.DocumentStatusID,ig.Abbreviation as IGroupAbbreviation,
u.Username, j.JDAbbreviation, inf.DocumentName,
it.Abbreviation as ITypeAbbreviation, d.DocumentDate, ds.Abbreviation as DocumentStatusAbbreviation,
dbo.GetFlatDocumentName(d.DocumentID) as FlatDocumentName
From Documents d Inner 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
Inner Join Jurisdictions j on j.JurisdictionID = d.JurisdictionID
) as peta_query) peta_paged WHERE peta_rn>12000 AND peta_rn<=12100
但是,第二个查询在 1 秒内执行:
SELECT * FROM (SELECT
peta_query.* From
(
Select ROW_NUMBER() OVER (Order By d.DocumentID desc) peta_rn, d.DocumentID, d.IsReEfiled, d.IGroupID, d.ITypeID, d.RecordingDateTime, dbo.GetLatestStatusDateTime(d.DocumentID) as LatestStatusDatetime,
dbo.FnCanChangeDocumentStatus(d.DocumentStatusID,d.DocumentID) as CanChangeStatus, d.IDate, d.InstrumentID, d.DocumentStatusID,ig.Abbreviation as IGroupAbbreviation,
u.Username, j.JDAbbreviation, inf.DocumentName,
it.Abbreviation as ITypeAbbreviation, d.DocumentDate, ds.Abbreviation as DocumentStatusAbbreviation,
dbo.GetFlatDocumentName(d.DocumentID) as FlatDocumentName
From Documents d Inner 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
Inner Join Jurisdictions j on j.JurisdictionID = d.JurisdictionID
) as peta_query) peta_paged WHERE peta_rn>12000 AND peta_rn<=12100
我的问题是:
1)您能否建议为什么我的第一个查询需要这么长时间才能执行?我想优化我的第一个查询,因为它是由一个自动化程序生成的(我确实有它的源,但我不能在 Select 中移动 RowNumber)。
2)执行时间也取决于 peta_rn。如果我给 peta_rn > 50000 和 peta_rn <= 50100,则执行所需的时间非常长。您能否还建议为什么执行时间取决于我尝试获取的行,因为最终我一次只尝试获取 100 行。