使用row_number()查询需要花费太多时间

时间:2012-10-17 03:49:57

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

我的数据库中有大约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&gt; 50000和peta_rn <= 50100,执行需要花费大量时间。你还可以请一下,为什么要执行的时间取决于我想要获取的行,因为最终我只想尝试一次获取100行。

1 个答案:

答案 0 :(得分:1)

只是为了它的乐趣 - 你能尝试这个CTE并看看它的表现如何?我不完全理解为什么你有三个嵌套的子查询......

;WITH PetaQuery AS
(
    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 OUTER JOIN 
        ITypes it On d.ITypeID = it.ITypeID 
    LEFT OUTER JOIN 
        Users u On u.UserID = d.UserID 
    LEFT OUTER JOIN 
        DocumentStatuses ds On d.DocumentStatusID = ds.DocumentStatusID 
    LEFT OUTER JOIN 
        InstrumentFiles inf On d.DocumentID = inf.DocumentID 
    INNER JOIN 
        Jurisdictions j on j.JurisdictionID = d.JurisdictionID                           
) 
SELECT * FROM PetaQuery
WHERE peta_rn > 12000 AND peta_rn <= 12100

这与原始查询的响应时间是否相同?

如果是,请检查:

  • 您是否拥有所有外键的索引(IGroupIDITypeIDUserIDDocumentStatusIDDocumentIDJurisdictionID)?这对于快速JOIN表现至关重要