1

我有下面的存储过程,它在不返回总记录的情况下工作得很好,但是当我在单独的查询中返回总记录时,它变得很慢。

有没有另一种方法可以更快地返回总记录

视图是

SELECT     dbo.tbl_ImagesMaster.ImgTitle, dbo.tbl_ImagesMaster.ImgID, dbo.tbl_ImagesMaster.ImgDate, dbo.tbl_ImagesMaster.ImgActive, 
                      dbo.tbl_ImagesMaster.ImgHits, dbo.aspnet_Users.UserName, dbo.tbl_ImagesMaster.ImgUserID, dbo.tbl_ImagesDetails.ImgDPath, 
                      dbo.tblUsersExtended.UAvatar, dbo.tbl_ImagesMaster.ImgRemarks
FROM         dbo.tbl_ImagesMaster INNER JOIN
                      dbo.aspnet_Users ON dbo.tbl_ImagesMaster.ImgUserID = dbo.aspnet_Users.UserId INNER JOIN
                      dbo.tbl_ImagesDetails ON dbo.tbl_ImagesMaster.ImgID = dbo.tbl_ImagesDetails.ImgDParentID INNER JOIN
                      dbo.tblUsersExtended ON dbo.aspnet_Users.UserId = dbo.tblUsersExtended.UUserID
WHERE     (dbo.tbl_ImagesDetails.ImgDDefault = 1)

SP

ALTER Procedure [dbo].[usp_searchImagestest] 

(
@CurrentPage As int=1,
@PageSize As int=10,
@searchType As int=1,
@Activeflag As bit=null,
@searchTerm as nvarchar(200),
@TotalRecords As int OUTPUT
)
As
begin
-- Turn off count return.
Set NoCount On


    --Full text search
    if @searchType=1
    begin
        select 
            ImgID,ImgTitle,ImgDate,imgHits,UserName,uavatar,RowRank
        from
            (Select
                ImgID,imgtitle,ImgDate,imghits,UserName,UAvatar
                , ROW_NUMBER() OVER (ORDER BY ImgID desc) AS RowRank
            From
                 vw_MasterImagesSearch
                WHERE FREETEXT(*, @searchTerm) and (@Activeflag IS NULL or ImgActive=@Activeflag)
            ) as MyImages
        where

            RowRank > (@PageSize * (@CurrentPage-1)) AND RowRank <= (@CurrentPage * @PageSize)

-- TOTAL RECORD, WHICH SLOW DOWN THE WHOLE SP EXECUTION
        select @TotalRecords = (select COUNT(*) From vw_MasterImagesSearch 
        WHERE FREETEXT(*, @searchTerm) and (@Activeflag IS NULL or ImgActive=@Activeflag))
    end

编辑

我试过这个

查询 1

select 
        ImgID,ImgTitle,ImgDate,imgHits,RowRank,TOTALCOUNT,
        (select username from aspnet_Users where UserId=MyImages.imguserid) as username,
        (select UAvatar from tblUsersExtended where UUserID=MyImages.imguserid) as Uavatar

    from
        (Select
            ImgID,imgtitle,ImgDate,imghits,ImgUserID
            , ROW_NUMBER() OVER (ORDER BY ImgID desc) AS RowRank
            ,COUNT(*) OVER() AS TOTALCOUNT
        From
             tbl_ImagesMaster
            WHERE FREETEXT(ImgTitle, @searchTerm) and (@Activeflag IS NULL or ImgActive=@Activeflag)
        ) as MyImages
    where

        RowRank > (@PageSize * (@CurrentPage-1)) AND RowRank <= (@CurrentPage * @PageSize)

查询 2

    select 
        ImgID,ImgTitle,ImgDate,imgHits,RowRank,
        (select username from aspnet_Users where UserId=MyImages.imguserid) as username,
        (select UAvatar from tblUsersExtended where UUserID=MyImages.imguserid) as Uavatar

    from
        (Select
            ImgID,imgtitle,ImgDate,imghits,ImgUserID
            , ROW_NUMBER() OVER (ORDER BY ImgID desc) AS RowRank
        From
             tbl_ImagesMaster
            WHERE FREETEXT(ImgTitle, @searchTerm) and (@Activeflag IS NULL or ImgActive=@Activeflag)
        ) as MyImages
    where

        RowRank > (@PageSize * (@CurrentPage-1)) AND RowRank <= (@CurrentPage * @PageSize)

    select @TotalRecords = (select COUNT(*) From tbl_ImagesMaster
    WHERE FREETEXT(ImgTitle, @searchTerm) and (@Activeflag IS NULL or ImgActive=@Activeflag))

查询 1 用时 3 秒 查询 2 用时不到 1 秒

表中的总行数为 100,000 行

4

3 回答 3

3

第二个选择正在重新运行搜索。只需更改您的分页搜索:

SELECT 
    ImgID,ImgTitle,ImgDate,imgHits,UserName,uavatar,RowRank, TOTALCOUNT
FROM
    (SELECT
        ImgID,
        imgtitle,
        ImgDate,
        imghits,
        UserName,
        UAvatar, 
        ROW_NUMBER() OVER (ORDER BY ImgID desc) AS RowRank
        COUNT(*) OVER() AS TOTALCOUNT
    FROM
         vw_MasterImagesSearch
        WHERE FREETEXT(*, @searchTerm) and (@Activeflag IS NULL or ImgActive=@Activeflag)
    ) as MyImages
WHERE
    RowRank > (@PageSize * (@CurrentPage-1)) AND RowRank 

The line

COUNT(*) OVER() AS TOTALCOUNT

在分页发生之前为您提供内部 SELECT 的计数。

于 2012-06-06T20:46:07.353 回答
0

我认为应该改变 iKnowKungFoo 的解决方案部分

ROW_NUMBER() OVER (ORDER BY ImgID desc) AS RowRank
COUNT(*) OVER() AS TOTALCOUNT

ROW_NUMBER() OVER (ORDER BY ImgID desc) AS RowRank
ROW_NUMBER() OVER (ORDER BY ImgID) AS TotalRows

所以第二部分应该包含相反的顺序。您可以阅读此答案文章(需要注册到最后一篇)。

无论如何,我个人都会使用CTE而不是子查询。通过这种方式,您可以将所有行的主集定义为 CTE。您需要的只是执行一次获取COUNT(*)并另外获取您需要的数据页面WHERE seq BETWEEN (@PageSize * (@CurrentPage-1)) AND (@CurrentPage * @PageSize)

另一种选择可以得到CROSS JOIN方法而不是ROW_NUMBER() OVER (<reverse sort order>)(参见此处)或UNION方法(参见此处)。我建议您测试所有方法并找到最适合您的方法。

更新:我对 CTE 的意思是它可以更好地优化为视图。所以首先你可以试试下面的查询

WITH MasterImagesSearch AS (
    SELECT im.ImgTitle, im.ImgID, im.ImgDate, im.ImgActive,
           im.ImgHits, asp.UserName, im.ImgUserID, id.ImgDPath,
           e.UAvatar, im.ImgRemarks
    FROM dbo.tbl_ImagesMaster AS im INNER JOIN
           dbo.aspnet_Users AS asp ON im.ImgUserID = asp.UserId INNER JOIN
           dbo.tbl_ImagesDetails AS id ON im.ImgID = id.ImgDParentID INNER JOIN
           dbo.tblUsersExtended AS e ON asp.UserId = e.UUserID
    WHERE id .ImgDDefault = 1
), GetAll AS (
    SELECT ImgID,ImgTitle,ImgDate,imgHits,UserName,uavatar
    FROM MasterImagesSearch
    WHERE FREETEXT(ImgTitle, @searchTerm)
        AND (@Activeflag IS NULL or ImgActive=@Activeflag)
), MyImages AS (
    SELECT ImgID,imgtitle,ImgDate,imghitsUserName,uavatar
        ,ROW_NUMBER() OVER (ORDER BY ImgID desc) AS RowRank
        ,ROW_NUMBER() OVER (ORDER BY ImgID) AS TotalRows
    FROM GetAll
)
SELECT ImgID,imgtitle,ImgDate,imghitsUserName,uavatar
    ,RowRank + TotalRows -1 AS Total
FROM MyImages
WHERE RowRank BETWEEN (@PageSize * (@CurrentPage-1)) AND (@CurrentPage * @PageSize)

我没有做任何额外的优化,但我使用了 CTEINNER JOIN而不是子查询。

于 2012-06-06T23:31:36.793 回答
0

我认为当您使用存储过程获取带参数的查询结果时,SQL Server 会为此 SP 生成执行计划,并为您以后调用的每个 SP 使用此生成的执行计划。生成执行计划的有趣之处在于表统计信息,SQL Server 使用它们来生成最佳执行计划。如果您的 SP 的参数发生更改,可能会生成无用的执行计划,也不是最佳执行计划。我认为每次想要使用 whit defrence 参数来计算执行时间时,您都必须重新编译您的 SP。此外,您可以使用 SP 中的内部参数来为每次运行 SP 时由 SQL Server 自动生成执行计划。例如使用下面的代码:

ALTER Procedure [dbo].[usp_searchImagestest]
( 
  @CurrentPage As int=1, 
  @PageSize As int=10, 
  @searchType As int=1, 
  @Activeflag As bit=null, 
  @searchTerm as nvarchar(200), 
  @TotalRecords As int OUTPUT 
) 
As begin

Declare       
  @ICurrentPage int, 
  @IPageSize int, 
  @IsearchType int, 
  @IActiveflag bit,
  @IsearchTerm nvarchar(200), 
  @ITotalRecords int 

Set @ICurrentPage = @CurrentPage
Set @IPageSize = @PageSize
Set @IsearchType = @searchType
Set @IActiveflag = Activeflag
Set @IsearchTerm = searchTerm
Set @ITotalRecords = @TotalRecords

然后使用内部参数而不是SP参数。

于 2012-06-07T12:11:34.727 回答