我有一个查询:
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
问题是,我需要在查询中进行此关键字比较。现在我的问题是,如何更改原始查询以获得更好的性能? 提示:
int
int
nvarchar(2000)
答案 0 :(得分:0)
在第二个查询使用的索引中包含Keyword
。