这是我的查询,大约需要 5 秒来获取 50 条记录。DB 中的总行数约为 100K。
exec sp_executesql N'
set arithabort off;
set transaction isolation level read uncommitted;
With cte
as (
Select
ROW_NUMBER() OVER (Order By d.LastStatusChangedDateTime desc ) peta_rn,
d.DocumentID
From Documents d
Inner Join Users u on d.UserID = u.UserID
Inner Join IGroupes ig on ig.IGroupID = d.IGroupID
Inner Join ITypes it on it.ITypeID = d.ITypeID
Where 1=1
And DateDiff(day, @0, d.LastStatusChangedDateTime ) >= 0
And DateDiff(day, d.LastStatusChangedDateTime, @1) >= 0
And (d.JurisdictionID = @2
Or DocumentStatusID = @3
Or DocumentStatusID = @4
Or DocumentStatusID = @5
Or DocumentStatusID = @6)
)
Select
d.DocumentID,
d.IsReEfiled,
d.IGroupID,
d.ITypeID,
d.RecordingDateTime,
d.CreatedByAccountID,
d.JurisdictionID,
d.LastStatusChangedDateTime as LastStatusChangedDateTime,
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,
ds.Name as DocumentStatusName,
(Select
Cast(
Case
When d.DocumentID = ( Select Top 1 doc.DocumentID
From Documents doc
Where doc.JurisdictionID = d.JurisdictionID
And doc.DocumentStatusID = d.DocumentStatusID
Order By LastStatusChangedDateTime)
Then 1
Else 0
End as bit))
as CanChangeStatus,
Upper((
Select Top 1 Stuff((
Select ''='' + dbo.GetDocumentNameFromParams(Business, FirstName, MiddleName, LastName, Abbreviation, NameTypeID)
From DocumentNames dn
Left Join Titles t on dn.TitleID = t.TitleID
Where DocumentID = d.DocumentID
For XML PATH('''')),1,1,'''')))
as FlatDocumentName
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
Where
1=1
And peta_rn>=@13
AND peta_rn<=@14
Order
by peta_rn
',N'@0 datetime,@1 datetime,@2 int,@3 int,@4 int,@5 int,@6 int,@7 int,@8 int,@9 int,@10 int,@11 int,@12 int,@13 int,@14 int',
@0='2013-03-01 00:00:00',@1='2013-04-30 00:00:00',@2=1,@3=5,@4=9,@5=4,@6=1,@7=1,@8=5,@9=9,@10=4,@11=1,@12=1,@13=0,@14=50
我确认需要时间的部分是这个罪魁祸首子查询:
(Select
Cast(
Case
When d.DocumentID = ( Select Top 1 doc.DocumentID
From Documents doc
Where doc.JurisdictionID = d.JurisdictionID
And doc.DocumentStatusID = d.DocumentStatusID
Order By LastStatusChangedDateTime)
Then 1
Else 0
End as bit))
作为替代方案,我尝试使用此解决方案:
exec sp_executesql N'
set arithabort off;
set transaction isolation level read uncommitted;
With cte
as (
Select ROW_NUMBER() OVER (Order By d.LastStatusChangedDateTime desc ) peta_rn,
CASE WHEN ROW_NUMBER() OVER (
PARTITION BY d.JurisdictionID, d.DocumentStatusID ORDER BY LastStatusChangedDateTime ) = 1
THEN 1
ELSE 0
END
AS CanChangeStatus,
d.DocumentID
From Documents d
Inner Join Users u on d.UserID = u.UserID
Inner Join IGroupes ig on ig.IGroupID = d.IGroupID
Inner Join ITypes it on it.ITypeID = d.ITypeID
Where 1=1
And (d.JurisdictionID = @2
Or DocumentStatusID = @3
Or DocumentStatusID = @4
Or DocumentStatusID = @5
Or DocumentStatusID = @6)
)
Select
d.DocumentID,
d.IsReEfiled,
d.IGroupID,
d.ITypeID,
d.RecordingDateTime,
d.CreatedByAccountID,
d.JurisdictionID,
d.LastStatusChangedDateTime as LastStatusChangedDateTime,
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,
ds.Name as DocumentStatusName,
cte.CanChangeStatus,
Upper((
Select Top 1 Stuff((
Select ''='' + dbo.GetDocumentNameFromParams(Business, FirstName, MiddleName, LastName, Abbreviation, NameTypeID)
From DocumentNames dn
Left Join Titles t on dn.TitleID = t.TitleID
Where DocumentID = d.DocumentID
For XML PATH('''')),1,1,'''')))
as FlatDocumentName
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
Where
1=1
And peta_rn>=@13
AND peta_rn<=@14
Order
by peta_rn
',N'@0 datetime,@1 datetime,@2 int,@3 int,@4 int,@5 int,@6 int,@7 int,@8 int,@9 int,@10 int,@11 int,@12 int,@13 int,@14 int',
@0='2010-03-01 00:00:00',@1='2013-04-30 00:00:00',@2=1,@3=5,@4=9,@5=4,@6=1,@7=1,@8=5,@9=9,@10=4,@11=1,@12=1,@13=0,@14=50
但它仍然需要超过 4 秒。理想情况下,我希望在 1 秒内得到结果。有什么办法可以加快这个速度吗?我已经在所有搜索列上都有索引,其中包括JurisdictionID
和DocumentStatusID