MS SQL 中 CONTAINS 条件的计划错误
Sql server 2008 R2
我有两个查询,
DECLARE @title NVARCHAR(500) = '"Finite" AND "Elements"'
select * from papers p
where (@title = '""' OR CONTAINS(p.name, @title))
select * from papers p
where (CONTAINS(p.name, @title))
第一个查询大约 7 秒,第二个查询几毫秒。为什么????
Sql server 2008 R2
I have two queries
DECLARE @title NVARCHAR(500) = '"Finite" AND "Elements"'
select * from papers p
where (@title = '""' OR CONTAINS(p.name, @title))
select * from papers p
where (CONTAINS(p.name, @title))
first works about 7 seconds, second several miliseconds. WHY????
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
@title = '""' OR ...
位是问题所在。在编译时,它不知道
@title
实际上是否包含值""
,因此是否需要返回所有行。您可以尝试将
OPTION(RECOMPILE)
添加到查询中,以便在分配变量后重新编译它,或者将其分为两种情况。The
@title = '""' OR ...
bit is the problem.At compile time it does not know whether
@title
is in fact going to contain the value""
and thus need to return all rows or not.You could try adding
OPTION(RECOMPILE)
to the query so it is recompiled after the variable is assigned or just splitting it up into two cases.