当其中包含字符串比较时,外部应用需要很长时间

时间:2019-02-27 13:16:41

标签: sql sql-server tsql outer-apply

我有一个查询:

with cte as
(   
//some select statement
)
    select -
    // also some code here
     from cte a 
     outer apply
(select top 1 position,traffic,tags,url,Domain,Load_Date,trend_date
    from cte b 
    where b.Date_ID<=a.Date_ID and 
        b.Load_Date is not null and 
        a.Domain is null and 
        a.Project_Id=b.Project_Id and
        a.SE_Id=b.SE_Id  and
        a.Keyword=b.keyword 
        order by a.Date_ID desc
        )x

我的cte返回近300万行。此查询需要很长时间才能完成(每4分钟仅返回500行)

但是以下没有外部比较的Keyword查询非常快:

with cte as
(   
//some select statement
)
    select 
    // also some code here
     from cte a 
     outer apply
(select top 1 position,traffic,tags,url,Domain,Load_Date,trend_date
    from cte b 
    where b.Date_ID<=a.Date_ID and 
        b.Load_Date is not null and 
        a.Domain is null and 
        a.Project_Id=b.Project_Id and
        a.SE_Id=b.SE_Id  and
        order by a.Date_ID desc
        )x

问题是,我需要在查询中进行此关键字比较。现在我的问题是,如何更改原始查询以获得更好的性能? 提示:

  • Project_Id为int
  • SE_Id为int
  • 关键字是nvarchar(2000)

1 个答案:

答案 0 :(得分:0)

在第二个查询使用的索引中包含Keyword