我有一个查询:
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)