SQL Row_Number()非常慢

时间:2015-08-20 10:02:53

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

我在SQL中有一个复杂的查询,由于SQL中Row_Number()而产生结果需要很长时间,即超过1分钟。我无法找到更好的选择来取代它。下面是我的SQL查询。

Declare  @AgreementNumber nvarchar(500)
     Declare   @SerialNo nvarchar(100)
     Declare    @EmailId nvarchar(1000)
     Declare    @CountryId nvarchar(10)
     Declare    @SaleType tinyint
     Declare    @CompanyName nvarchar(255)
     Declare    @PONo numeric(18,0)
     Declare   @Status tinyint
     Declare   @POCDeliveryType nvarchar(10)
     Declare   @FromDate datetime
     Declare   @ToDate datetime
     Declare    @CurrentPage numeric
     Declare    @PageSize numeric
     Declare   @FileId       numeric(18,0)
    Declare     @PurchaseOrg nvarchar(4)   

set @AgreementNumber =''
     set @SerialNo =''
     set @EmailId =''
     set @CountryId ='0014'
     set @SaleType=0
     set @CompanyName =''
     set @PONo= 0
     set @Status =0
     set @POCDeliveryType =''
     set @FromDate =''
     set @ToDate =''
     set @CurrentPage =1
     set @PageSize =100000
     set @FileId     =0
    set @PurchaseOrg ='' 



       ----For Paging---------------------------------------------------------------------
       Declare @TotalRecord numeric
       Declare @StartRow numeric
       Declare @EndRow numeric
       Set @StartRow=(@PageSize * (@CurrentPage-1)) + 1
       Set @EndRow=(@PageSize * (@CurrentPage)) 
       -----------------------------------------------------------------------------------

       Select @TotalRecord=Count(*)
       From OrderHeader OH inner join OrderLine OL on OH.OrderID=OL.OrderId
       Where (OH.CountryId=@CountryId or ISNULL(@CountryId,'')='') and
       (OH.PurchaseOrg=@PurchaseOrg or ISNULL(@PurchaseOrg,'')='') and
              (SaleType=@SaleType or ISNULL(@SaleType,0)=0) and
              (CompanyName like '%'+ isnull(@CompanyName,'') + '%'or isnull(@CompanyName,'')='')and
              (PurchaseOrderNo = @PONo or isnull(@PONo,0)=0)and
              (OL.Status=@Status or ISNULL(@Status,0)=0) and
              (POCDeliveryPreference=@POCDeliveryType or ISNULL(@POCDeliveryType,'')='') and
              ((CONVERT(VARCHAR(12),OH.Created,112)>=CONVERT(VARCHAR(12),@FromDate,112))or (Isnull(@FromDate,'')=''))and
              (CONVERT(VARCHAR(12),OH.Created,112)<=CONVERT(VARCHAR(12),@ToDate,112)or (Isnull(@ToDate,'')='')) and
              (OL.SerialNumber like '%'+@SerialNo+'%' or ISNULL(@SerialNo,'') ='') and
              (OL.AgreementNumber like '%'+@AgreementNumber+'%' or ISNULL(@AgreementNumber,'') ='') and
              (OH.EmailAddress like '%'+@EmailId+'%' or ISNULL(@EmailId,'') ='') and
              (OH.FileId=@FileId or isnull(@FileId,0)=0)

       Select *, @TotalRecord as TotalRecord From
       (
              **Select ROW_NUMBER()  OVER 
              ( 
                     Order By OH.Created Desc
              )as Row**,OH.OrderID,ISNULL(CustomerId,'') as CustomerId,ISNULL(OH.CountryId,'') as CountryId,SaleType,HwPurchaseDate,ISNULL(AddressLine1,'') as
                     AddressLine1,ISNULL(AddressLine2,'') as AddressLine2,ISNULL(City,'') as City,ISNULL([State],'') as [State],ISNULL(Zip,'') as Zip,
                     ISNULL(County,'') as County,ISNULL(CompanyName,'') as CompanyName,ISNULL(EmailAddress,'') as EmailId,ISNULL(FirstName,'') as FirstName,
                     ISNULL(LastName,'') as LastName,ISNULL(PrimaryPhone,'') as Phone,ISNULL(PurchaseOrderNo,0) as POno,ISNULL(OL.[Status],0) as [Status],POCDeliveryPreference,
                     POCLanguage,CustPOReference,ISNULL(CurrencyCode,'') as CurrencyCode,TransactionId,ISNULL(OH.DeliveryStatus,0) as DeliveryStatus,
                     isnull(IPAddress,'') as IPAddress, ISNULL(InTouchSessionId,'') as InTouchSessionId, ISNULL(InTouchUserId,'') as InTouchUserId,
                     ISNULL(SourceSystem,0) as SourceSystem,SalesOrderDate,OH.Created,isnull(OL.SerialNumber,'') as SerialNumber,ISNULL(OL.AgreementNumber,'') as AgreementNumber,
                     ISNULL(OL.PurchaseOrderNoLineLevel,'') as PurchaseOrderNoLineLevel,ISNULL(OL.ProductName,'') as ProductName,ISNULL(OL.OrderQty,0) as OrderQty,
                     ISNULL(OL.ManufPartNo,'') as ManufPartNo,isnull(OL.VendorStatus,'') as VendorStatus, isnull(OL.StatusRemark, '') as StatusRemark

              From OrderHeader OH inner join OrderLine OL on OH.OrderID=OL.OrderId
              Where (OH.CountryId=@CountryId or ISNULL(@CountryId,'')='') and
              (OH.PurchaseOrg=@PurchaseOrg or ISNULL(@PurchaseOrg,'')='') and
              (SaleType=@SaleType or ISNULL(@SaleType,0)=0) and
              (CompanyName like '%'+ isnull(@CompanyName,'') + '%'or isnull(@CompanyName,'')='')and
              (PurchaseOrderNo = @PONo or isnull(@PONo,0)=0)and
              (OL.Status=@Status or ISNULL(@Status,0)=0) and
              (POCDeliveryPreference=@POCDeliveryType or ISNULL(@POCDeliveryType,'')='') and
              ((CONVERT(VARCHAR(12),OH.Created,112)>=CONVERT(VARCHAR(12),@FromDate,112))or (Isnull(@FromDate,'')=''))and
              (CONVERT(VARCHAR(12),OH.Created,112)<=CONVERT(VARCHAR(12),@ToDate,112)or (Isnull(@ToDate,'')='')) and
              (OL.SerialNumber like '%'+@SerialNo+'%' or ISNULL(@SerialNo,'') ='') and
              (OL.AgreementNumber like '%'+@AgreementNumber+'%' or ISNULL(@AgreementNumber,'') ='') and
              (OH.EmailAddress like '%'+@EmailId+'%' or ISNULL(@EmailId,'') ='') and
              (OH.FileId=@FileId or isnull(@FileId,0)=0)
       )
       as RowResults
       Where Row between @StartRow AND  @EndRow

我已经强调了那个花了很长时间的部分。

3 个答案:

答案 0 :(得分:1)

OH.CountryId = @CountryId or ISNULL(@CountryId, '') = '')

通常应该避免这些事情。它可以防止SQL Server执行索引搜索,而是执行索引扫描。您是否可以使用动态SQL并仅在where OH.CountryId = @CountryId指定时包含在其中?

答案 1 :(得分:0)

替换

((CONVERT(VARCHAR(12),OH.Created,112)>=CONVERT(VARCHAR(12),@FromDate,112))

(CONVERT(VARCHAR(12),OH.Created,112)<=CONVERT(VARCHAR(12),@ToDate,112)

Cast(OH.Created as date)>=Cast(@FromDate as date) Cast(OH.Created as date)<=Cast(@FromDate as date)

转换创建一个不是最好的计划。

答案 2 :(得分:0)

查询本身存在很多问题

(1)两次使用相同的查询

(2)试图捕捉所有可能产生可怕计划的查询(效率低下)

(3)使用像OH.EmailAddress这样的谓词,如'%'+ @ EmailId +'%'

(4)低效的分页

Declare  @AgreementNumber nvarchar(500)
     Declare   @SerialNo nvarchar(100)
     Declare    @EmailId nvarchar(1000)
     Declare    @CountryId nvarchar(10)
     Declare    @SaleType tinyint
     Declare    @CompanyName nvarchar(255)
     Declare    @PONo numeric(18,0)
     Declare   @Status tinyint
     Declare   @POCDeliveryType nvarchar(10)
     Declare   @FromDate datetime
     Declare   @ToDate datetime
     Declare    @CurrentPage numeric
     Declare    @PageSize numeric
     Declare   @FileId       numeric(18,0)
    Declare     @PurchaseOrg nvarchar(4)   

set @AgreementNumber =''
     set @SerialNo =''
     set @EmailId =''
     set @CountryId ='0014'
     set @SaleType=0
     set @CompanyName =''
     set @PONo= 0
     set @Status =0
     set @POCDeliveryType =''
     set @FromDate =''
     set @ToDate =''
     set @CurrentPage =1
     set @PageSize =100000
     set @FileId     =0
    set @PurchaseOrg ='' 



       ----For Paging---------------------------------------------------------------------
       Declare @TotalRecord numeric
       Declare @StartRow numeric
       Declare @EndRow numeric
       Set @StartRow=(@PageSize * (@CurrentPage-1)) + 1
       Set @EndRow=(@PageSize * (@CurrentPage)) 
       -----------------------------------------------------------------------------------

       --Select @TotalRecord=Count(*)
       --From OrderHeader OH inner join OrderLine OL on OH.OrderID=OL.OrderId
       --Where (OH.CountryId=@CountryId or ISNULL(@CountryId,'')='') and
       --(OH.PurchaseOrg=@PurchaseOrg or ISNULL(@PurchaseOrg,'')='') and
       --       (SaleType=@SaleType or ISNULL(@SaleType,0)=0) and
       --       (CompanyName like '%'+ isnull(@CompanyName,'') + '%'or isnull(@CompanyName,'')='')and
       --       (PurchaseOrderNo = @PONo or isnull(@PONo,0)=0)and
       --       (OL.Status=@Status or ISNULL(@Status,0)=0) and
       --       (POCDeliveryPreference=@POCDeliveryType or ISNULL(@POCDeliveryType,'')='') and
       --       ((CONVERT(VARCHAR(12),OH.Created,112)>=CONVERT(VARCHAR(12),@FromDate,112))or (Isnull(@FromDate,'')=''))and
       --       (CONVERT(VARCHAR(12),OH.Created,112)<=CONVERT(VARCHAR(12),@ToDate,112)or (Isnull(@ToDate,'')='')) and
       --       (OL.SerialNumber like '%'+@SerialNo+'%' or ISNULL(@SerialNo,'') ='') and
       --       (OL.AgreementNumber like '%'+@AgreementNumber+'%' or ISNULL(@AgreementNumber,'') ='') and
       --       (OH.EmailAddress like '%'+@EmailId+'%' or ISNULL(@EmailId,'') ='') and
       --       (OH.FileId=@FileId or isnull(@FileId,0)=0)





       With RowResults as

      (

        Select ROW_NUMBER()  OVER 
              ( 
                     Order By OH.Created Desc
              )as Row ,OH.OrderID,ISNULL(CustomerId,'') as CustomerId,ISNULL(OH.CountryId,'') as CountryId,SaleType,HwPurchaseDate,ISNULL(AddressLine1,'') as
                     AddressLine1,ISNULL(AddressLine2,'') as AddressLine2,ISNULL(City,'') as City,ISNULL([State],'') as [State],ISNULL(Zip,'') as Zip,
                     ISNULL(County,'') as County,ISNULL(CompanyName,'') as CompanyName,ISNULL(EmailAddress,'') as EmailId,ISNULL(FirstName,'') as FirstName,
                     ISNULL(LastName,'') as LastName,ISNULL(PrimaryPhone,'') as Phone,ISNULL(PurchaseOrderNo,0) as POno,ISNULL(OL.[Status],0) as [Status],POCDeliveryPreference,
                     POCLanguage,CustPOReference,ISNULL(CurrencyCode,'') as CurrencyCode,TransactionId,ISNULL(OH.DeliveryStatus,0) as DeliveryStatus,
                     isnull(IPAddress,'') as IPAddress, ISNULL(InTouchSessionId,'') as InTouchSessionId, ISNULL(InTouchUserId,'') as InTouchUserId,
                     ISNULL(SourceSystem,0) as SourceSystem,SalesOrderDate,OH.Created,isnull(OL.SerialNumber,'') as SerialNumber,ISNULL(OL.AgreementNumber,'') as AgreementNumber,
                     ISNULL(OL.PurchaseOrderNoLineLevel,'') as PurchaseOrderNoLineLevel,ISNULL(OL.ProductName,'') as ProductName,ISNULL(OL.OrderQty,0) as OrderQty,
                     ISNULL(OL.ManufPartNo,'') as ManufPartNo,isnull(OL.VendorStatus,'') as VendorStatus, isnull(OL.StatusRemark, '') as StatusRemark

              From OrderHeader OH inner join OrderLine OL on OH.OrderID=OL.OrderId
              Where (OH.CountryId=@CountryId or ISNULL(@CountryId,'')='') and
              (OH.PurchaseOrg=@PurchaseOrg or ISNULL(@PurchaseOrg,'')='') and
              (SaleType=@SaleType or ISNULL(@SaleType,0)=0) and
              (CompanyName like '%'+ isnull(@CompanyName,'') + '%'or isnull(@CompanyName,'')='')and
              (PurchaseOrderNo = @PONo or isnull(@PONo,0)=0)and
              (OL.Status=@Status or ISNULL(@Status,0)=0) and
              (POCDeliveryPreference=@POCDeliveryType or ISNULL(@POCDeliveryType,'')='') and
              ((CONVERT(VARCHAR(12),OH.Created,112)>=CONVERT(VARCHAR(12),@FromDate,112))or (Isnull(@FromDate,'')=''))and
              (CONVERT(VARCHAR(12),OH.Created,112)<=CONVERT(VARCHAR(12),@ToDate,112)or (Isnull(@ToDate,'')='')) and
              (OL.SerialNumber like '%'+@SerialNo+'%' or ISNULL(@SerialNo,'') ='') and
              (OL.AgreementNumber like '%'+@AgreementNumber+'%' or ISNULL(@AgreementNumber,'') ='') and
              (OH.EmailAddress like '%'+@EmailId+'%' or ISNULL(@EmailId,'') ='') and
              (OH.FileId=@FileId or isnull(@FileId,0)=0)
       )


Select max (Row) , * FRom      RowResults
 Where Row between @StartRow AND  @EndRow
 option (recompile)
 -- quick fix only 



-- --if using SQL SERVER 2012
--  OFFSET @StartRow ROWS
--FETCH NEXT @EndRow -@StartRow  ROWS ONLY





   --if  ans still not gaining performance we wold like to see actual exection plan 

- (1)两次使用相同的查询DONE

- (2)尝试捕获所有查询(效率低下),可以通过选项重新编译产生可怕的计划DONE(如果你没有暴露于sql注入,你还需要理解这个使用动态查询)

- (3)使用类似'%'+ @ EmailId +'%'的OH.EmailAddress这样的谓词 - 你需要解决这个问题

- (4)效率低下的分页 - 下一次获取与row_num相当快。