Parameter sniffing

sakuraime 2,326 Reputation points
2020-09-03T03:35:49.667+00:00

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 ?

SQL Server
SQL Server
A family of Microsoft relational database management and analysis systems for e-commerce, line-of-business, and data warehousing solutions.
13,770 questions
0 comments No comments
{count} votes

4 answers

Sort by: Most helpful
  1. Uri Dimant 206 Reputation points
    2020-09-03T04:01:12.667+00:00

    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/

    0 comments No comments

  2. CathyJi-MSFT 22,201 Reputation points Microsoft Vendor
    2020-09-03T06:58:14.61+00:00

    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


  3. Shashank Singh 6,251 Reputation points
    2020-09-03T07:04:12.083+00:00

    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

    the-sniffing-database
    query-plan-mysteries.html


  4. Erland Sommarskog 111.1K Reputation points
    2020-09-03T22:17:52.56+00:00

    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.

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.