大型表分页查询的计算列检查性能
我有这个查询来检索(见下文,为简单起见简称)职位搜索数据。我们正在处理大约一百万条记录。
Select ID
from
(
Select ID,createDate
,SearchKeyMatchRank
,Row_Number() over(Order By createDate) As rowNumber
from Jobs J
OUTER APPLY
(
Select SearchKeyMatchRank=
CASE WHEN @searchKey='""' THEN 0
ELSE
(Select IsNull([RANK],0) from FREETEXTTABLE(Jobs,title,@searchKey) Where [Key]=J.ID)*4
+(Select IsNull([RANK],0) from FREETEXTTABLE(Jobs,description,@searchKey) Where [Key]=J.ID)*4
+(
select SUM(ISNULL(JS2.[Rank],0))
from FREETEXTTABLE(JobSkills,skill,@searchKey) JS2
Where JS2.[Key] in (Select ID from JobSkills Where jobId=J.Id)
)*2
END
) SMR
Where
SearchKeyMatchRank>0 --simplified here
) T2
where
rowNumber>=CASE WHEN @startIndex>0 AND @endIndex>0 THEN @startIndex ELSE rowNumber END
AND rowNumber<=CASE WHEN @startIndex>0 AND @endIndex>0 THEN @endIndex ELSE rowNumber END
注意:
我无法使用常规联接,因为我需要通过将 jobId 传递给 REETEXTTABLE 来查找加权排名。
问题:
速度非常慢。
显然问题是比较计算列。
SearchKeyMatchRank>0
如果我起飞 其中SearchKeyMatchRank>0 只需不到一秒钟。
有人知道我们如何改进这个吗?
I have this query to retrieve (see below, made short for simplicity) search data for jobs. We are dealing with about a million records.
Select ID
from
(
Select ID,createDate
,SearchKeyMatchRank
,Row_Number() over(Order By createDate) As rowNumber
from Jobs J
OUTER APPLY
(
Select SearchKeyMatchRank=
CASE WHEN @searchKey='""' THEN 0
ELSE
(Select IsNull([RANK],0) from FREETEXTTABLE(Jobs,title,@searchKey) Where [Key]=J.ID)*4
+(Select IsNull([RANK],0) from FREETEXTTABLE(Jobs,description,@searchKey) Where [Key]=J.ID)*4
+(
select SUM(ISNULL(JS2.[Rank],0))
from FREETEXTTABLE(JobSkills,skill,@searchKey) JS2
Where JS2.[Key] in (Select ID from JobSkills Where jobId=J.Id)
)*2
END
) SMR
Where
SearchKeyMatchRank>0 --simplified here
) T2
where
rowNumber>=CASE WHEN @startIndex>0 AND @endIndex>0 THEN @startIndex ELSE rowNumber END
AND rowNumber<=CASE WHEN @startIndex>0 AND @endIndex>0 THEN @endIndex ELSE rowNumber END
NOTES:
I cannot use a regular join since I would need to find weighted ranks by passing the jobId to REETEXTTABLE.
PROBLEM:
Its extremely slow.
Apparently the problem is comparing a computed column.
SearchKeyMatchRank>0
If I take off
Where SearchKeyMatchRank>0
it takes less then a second.
Anybody got an idea how we could improve this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
将列从计算列更改为“常规”列。在您的测试环境中尝试一下,看看性能改进是否相同。
Change the column from a computed column to a "regular" column. Try it in your test environment and see if the performance improvement is the same.
如果列使用非确定性函数,我们采取的方法是在表上定义一个普通列并添加插入/更新触发器来更新值。这样,当更改依赖字段或添加新记录时,会有轻微的影响,但不会影响查询性能,因为该列是标准 SQL 列。它还可以轻松地建立索引。
If the column uses a non-deterministic function, the approach we've taken is to define a normal column on the table and add an insert/update trigger to updated the value. In this way, there will be a slight hit when a dependent field is changed or a new record is added, but it will not impact on query performance as the column is a standard SQL column. It can also be indexed easily.