Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sp_executesql is slow with parameters

Tags:

I'm using dapper-dot-net as an ORM and it produces the following, slow-executing (1700ms), SQL code.

exec sp_executesql N'SELECT TOP 5 SensorValue FROM "Values"     WHERE DeviceId IN (@id1,@id2) AND SensorId = @sensor         AND SensorValue != -32768 AND SensorValue != -32767',N'@id1             bigint,@id2 bigint,@sensor int',@id1=139,@id2=726,@sensor=178 

When I modify this code by removing the parameters the query executes blazingly fast (20ms). Should the lack of these parameters actually make this big difference and why?

exec sp_executesql N'SELECT TOP 5 SensorValue FROM "Values"     WHERE DeviceId IN (139,726) AND SensorId = 178         AND SensorValue != -32768 AND SensorValue != -32767' 
like image 706
m__ Avatar asked Jun 07 '12 13:06

m__


People also ask

Is Sp_executesql faster?

This requires an immediate caveat. You should absolutely be using sp_executesql over any type of non-parameterized execution of T-SQL.

What are the benefits of using Sp_executesql over exec?

sp_executesql allows for statements to be parameterized, Therefore It's more secure than EXEC in terms of SQL injection.

What is difference between exec and Sp_executesql?

sp_executesql supports parameterisation, whereas EXEC only accepts a string. Only performance differences that may arise are due to the parameterisation i.e. a parameterised sp_executesql call is more likely to have a reusable cached plan.

Can we use Sp_executesql in function?

It appears that you can't. You can execute extended stored procedure inside a function and, even though sp_executesql is an extended stored procedure (despite its name), it still generates the message "only functions and extended stored procedures can be executed within a function".


1 Answers

Add OPTION (RECOMPILE) to the end

... AND SensorValue != -32767 OPTION (RECOMPILE)  

I suspect you are experiencing "parameter sniffing"

If that's the case we can leave it with the OPTION or consider alternatives

Update 1

The following article will introduce you to "parameter sniffing" http://pratchev.blogspot.be/2007/08/parameter-sniffing.html

I advice that you get to know the ins and out because it will make you much better in understanding sql server internals (that can bite).

If you understand it you will know that the tradeoff with option recompile can be a performance decrease if the statement is executed very often.

I personally add option recompile after I know the root cause is parameter sniffing and leave it in unless there is a performance issue. Rewriting a statement to avoid bad parameter sniffing leads to loss of intent and this lowers maintainability. But there are cases when the rewrite is justified (use good comments when you do).

Update 2

The best read I had on the subject was in chapter 32 called "Parameter sniffing: your best friend... except when it isn't by " by GRANT FRITCHEY

It's recommended.

SQL Server MVP Deep Dives, Volume 2

like image 168
buckley Avatar answered Sep 25 '22 08:09

buckley