SQL 2008:
This is slow (takes 1 1/2 minutes):
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???
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
I would imagine that you must have a non covering index with leading column
comparepnfwdthat is used by the literal query but not by the query with the variable.You can use
OPTION (RECOMPILE)to get SQL Server to recompile the plan taking into account the actual variable value.