Please read this link
https://sqlperformance.com/2013/08/t-sql-queries/parameter-sniffing-embedding-and-the-recompile-options
/An ad-hoc statement can be parameterized by SQL Server without parameter values being sniffed/
This browser is no longer supported.
Upgrade to Microsoft Edge to take advantage of the latest features, security updates, and technical support.
May I know if Parameter sniffing will only happen on Store procedure (with parameter) and also prepared sql ??
will parameter sniffing happens on adhoc sql ?
Please read this link
https://sqlperformance.com/2013/08/t-sql-queries/parameter-sniffing-embedding-and-the-recompile-options
/An ad-hoc statement can be parameterized by SQL Server without parameter values being sniffed/
Hi @sakuraime ,
>if Parameter sniffing will only happen on Store procedure (with parameter)
No, it can also occurs on T-SQL Functions or when a batch is explicitly parameterized for reuse using sp_executesql etc.
>will parameter sniffing happens on adhoc sql ?
Yes. An ad-hoc T-SQL statement containing constant literal values. A statement parameterized in this way is also subject to parameter sniffing.
Please refer to the blog Parameter Sniffing, Embedding, and the RECOMPILE Options to get more information.
Best regards,
Cathy
Apart from stored procedures functions can also suffer from parameter sniffing. For prepared SQL if there is a parameter yes it can, I have read this but you will see this seldom. For Adhoc plans again if there is a parameter there can be sniffing.
Two blogs for you
Everything that has a parameter can be sniffed. So a prepared statement can run into parameter sniffing, and so can dynamic SQL executed through sp_executesql if there are parameters.
As for a pure ad-hoc statement, sniffing could be an issue if force parameterisation is in force. I don't think it can happen with simple parameterisation; if SQL Server finds that here are more than one plan choice, it gets second thought on the parameterisation.