简体   繁体   English

如果使用declare参数,sql就像查询一样慢但如果没有则快

[英]sql like query slow if using declare parameter but fast if not

SQL 2008: This is slow (takes 1 1/2 minutes): SQL 2008:这很慢(需要1 1/2分钟):

declare @p1 varchar(50)
set @p1 = '976j%'
select * from invsearch_query where comparepnfwd like @p1

This takes less than a second: 这需要不到一秒钟:

select * from invsearch_query where comparepnfwd like '976j%'

Why??? 为什么???

I would imagine that you must have a non covering index with leading column comparepnfwd that is used by the literal query but not by the query with the variable. 我想你必须有一个非覆盖索引,其中前导列comparepnfwd由文字查询使用,但不是由带有变量的查询使用。

You can use OPTION (RECOMPILE) to get SQL Server to recompile the plan taking into account the actual variable value. 您可以使用OPTION (RECOMPILE)让SQL Server重新编译计划,同时考虑实际的变量值。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM