1

大家好,如何使这个查询获得最佳性能,因为它会因大量记录而超时,请注意我在一些加入的表上创建了索引

如果有任何更新或任何方式以新方式重写此存储过程以使其运行更快或性能最佳。

 ALTER PROCEDURE [dbo].[LoadClosedRequests]
    @RequestBusinessUnitId int,
    @StatusId int,
    @ProdId int,
    @SortExp varchar(100),
    @RequestID int,
    @RequestNumber int,
    @User varchar(56)  
    AS

    SELECT  Request.RequestID,RequestStatusID,RequestStatusName,
           BusinessUnitName,
           RequestWorkFlowID,
          RequestDate,RequestHFMApplicationID as RequestApplication,[User].UserName as RequestUserUserName,User_SWFT.UserId as RequestUserUserId,[User].UserEmail as RequestUserUserEmail,-1 as RequestParentKey,'' as RequestDescription,'' as RequestInitiatorUserName, -1 as RequestInitiatorUserId,'' as RequestInitiatorUserEmail
    ,Request.RequestType as RequestType,workflow.productID,productname
    FROM 
    (
    select requestid,LatestStatusDate,RequestStatusID,RequestStatusName
    from 
    (
    SELECT [RequestID],Max(HistoryItemDate) as LatestStatusDate
    from historyitem inner join request on historyitemRequestid=requestid
    group by RequestID
    ) currentstatus
    inner join historyitem on RequestID=HistoryItemRequestID and historyitemdate=LatestStatusDate
    inner join RequestStatus on RequestStatus.RequestStatusID=HistoryItem.HistoryItemRequestStatusID and HistoryItem.HistoryItemRequestStatusID in(2,3,4,5,6,9,10)
    --order by requestid
    ) LatestStatus

    inner join request on LatestStatus.requestid=Request.RequestID
    inner join Request on Request.RequestID=Request.RequestID
    inner join HistoryItem on HistoryItem.HistoryItemRequestID=Request.[RequestID]
    inner join User on UserID=RequestUser
    inner join [user] on [user].UserID=User.userid
    inner join WorkFlow on WorkFlowId=RequestWorkFlowID
    inner join BusinessUnit on BUID=BusinessUnitId
    inner Join Products on WorkFlow.ProductID= Products.ProductID


    where 
    (RequestStatusID=@StatusId or @StatusId=-1)and 
    (workflow.productID=@ProdId or @ProdId=-1)
    and (WorkFlow.buid=@RequestBusinessUnitId or @RequestBusinessUnitId=-1)
    and (Request.RequestID = @RequestNumber or @RequestNumber = -1)
    and (@User = '' or [User].UserName like '%'+ replace(@User, '*', '%') + '%' or exists (select distinct RequestID from requestuser inner join [User] on [User].userid = requestuser.userid where requestuser.requestID = Request.RequestID and [User].UserName like '%'+ replace(@User, '*', '%') + '%' ))

    group by Request.RequestID,RequestStatusID,RequestStatusName,
           BusinessUnitName,
           RequestWorkFlowID,
          RequestDate,RequestHFMApplicationID ,[User].UserName ,User.UserId,[User].UserEmail
    ,Request.RequestType,workflow.productID,productname

    ORDER BY 

    (
        CASE LOWER(@SortExp)

        WHEN 'requestuser' THEN  LOWER([User].[UserName]) 
        WHEN 'requestbu' THEN  LOWER(BusinessUnitName) 
        WHEN 'requeststatus' THEN  LOWER(RequestStatusName) 
        WHEN 'requestproduct' THEN  LOWER(productname) 
        WHEN 'requestapplication' THEN LOWER(CAST (RequestHFMApplicationID  AS VARCHAR) COLLATE SQL_Latin1_General_CP1_CI_AS)
        when 'requesttype'  THEN  
            ( 
                CASE LOWER([User].[UserName])
                WHEN 'Multi User' then 'Bulk'
                ELSE 'Single' 
                END
            )
        END
    ),

    (
        CASE LOWER(@SortExp) 
        WHEN 'requestdate' THEN RequestDate 
        END
    ),
    (
        CASE LOWER(@SortExp) 
        WHEN 'requestid' THEN Request.RequestID
        END
    )
4

0 回答 0