1

我编写了一个存储过程,它为给定范围提供了一组记录(例如,如果我给定起始索引 100 并且页面大小为 50,它会从给定起始索引返回 50 条记录。)。在我的表中记录更多(大约 14 个缺少),因此执行查询大约需要 13 秒。我想优化它。

这是我的查询-

SELECT  * FROM 
(           
  SELECT ROW_NUMBER() OVER( ORDER BY FE.CreatedDate DESC ) RowNumber,
      FE.FileEventId, MIN(FS.FolderSettingName) AS FolderSettingName,
      MIN(SD.StatusDescription) AS StatusDescription,
      MIN (@TotalRecords) as  RecordCount
  FROM FileEvent FE with (nolock)
  INNER JOIN  FolderSetting FS with (nolock) ON
                            FE.FolderSettingId = FS.FolderSettingId
  INNER JOIN  [Status] SD with (nolock) ON SD.StatusId = FE.StatusId
  WHERE FolderSettingName LIKE
                   ISNULL('%' + @FolderSettingName + '%' , FolderSettingName)
  AND FilePath LIKE ISNULL('%' + @FileName + '%' , FilePath)
  AND (@EventType IS NULL OR (ISNULL(FE.EventType, '') IN (SELECT * FROM
                       dbo.UFN_StringSplitByCommaSeparator(@EventType))))
  AND (@EventStatus IS NULL OR (ISNULL(SD.StatusDescription, '') IN
              (SELECT * FROM dbo.UFN_StringSplitByCommaSeparator(@EventStatus))))
  AND FE.ModifiedBy LIKE ISNULL('%' + @ProcessedBy + '%' , FE.ModifiedBy)
  AND FE.ModifiedDate BETWEEN @FileProcessedFromDate AND @FileProcessedToDate
  AND @LockByMachine IS NULL OR FE.LockedByMachine LIKE 
                               ISNULL('%' + @LockByMachine + '%' , 1)
  GROUP BY FileEventId, FE.CreatedDate
) FEvent
INNER JOIN  FileEvent with (nolock) ON FileEvent.FileEventId = FEvent.FileEventId
WHERE FEvent.RowNumber BETWEEN @StartIndex AND (@StartIndex + @PageSize) 

你能告诉我如何优化它。

4

0 回答 0