{"id":17151,"date":"2022-03-11T12:35:41","date_gmt":"2022-03-11T11:35:41","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/"},"modified":"2022-05-31T16:37:54","modified_gmt":"2022-05-31T14:37:54","slug":"how-to-solve-stored-procedure-parameter-sniffing-problems","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/","title":{"rendered":"How to solve Stored Procedure parameter sniffing problems"},"content":{"rendered":"<p><a href=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/Flag_sqlbits_2022.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"size-full wp-image-54890 aligncenter\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/Flag_sqlbits_2022.jpg\" alt=\"\" width=\"399\" height=\"174\" \/><\/a>Since Tuesday, I have the chance to follow the <a href=\"https:\/\/arcade.sqlbits.com\/\">SQLBits 2022<\/a> conference. Sadly, I have to do it remotely this year but I promise next time I will come in-person \ud83d\ude09<br \/>\nSQLBits is one of the largest SQL Server conference in the world with some famous speakers like Brent Ozar, Bob Ward, Erik Darling&#8230;<br \/>\nIn this blog post, I will come back on one of the tips Erik gave us during his training session of one day about <a href=\"https:\/\/arcade.sqlbits.com\/session-details\/?id=298232\">The Professional Performance Tuning Blueprint<\/a> and show you how dynamic SQL can save our life, here save the performance, when parameter sniffing is not good enough.<\/p>\n<p>We have a Stored Procedure, here called MySP, where we select the top 10 values from Posts table join to Users table where column called ParentID in the Posts table equals the supplied ParentID column.<br \/>\n<code>CREATE OR ALTER PROCEDURE<br \/>\ndbo.MySP<br \/>\n(<br \/>\n@ParentId int<br \/>\n)<br \/>\nAS<br \/>\nBEGIN<br \/>\nSET NOCOUNT, XACT_ABORT ON;<br \/>\nSET STATISTICS XML ON;<\/p>\n<p>SELECT TOP (10)<br \/>\nu.DisplayName,<br \/>\np.*<br \/>\nFROM dbo.Posts AS p<br \/>\nJOIN dbo.Users AS u<br \/>\nON p.OwnerUserId = u.Id<br \/>\nWHERE p.ParentId = @ParentId<br \/>\nORDER BY u.Reputation DESC;<\/p>\n<p>SET STATISTICS XML OFF;<\/p>\n<p>END;<br \/>\nGO<\/code><br \/>\nWe have also a non-clustered index on the Posts table where index keys are ParentId and OwnerUserId:<br \/>\n<code><br \/>\nCREATE INDEX<br \/>\ngrmpf<br \/>\nON dbo.Posts<br \/>\n(ParentId, OwnerUserId)<br \/>\nWITH<br \/>\n(MAXDOP = 8, SORT_IN_TEMPDB = ON, DATA_COMPRESSION = PAGE);<br \/>\nGO<\/code><br \/>\nAfter having removed the plans in cache with DBCC FREEPROCCACHE, we run the Stored Procedure with ParendID equals to 184618 and check the execution time and query plan:<br \/>\n<a href=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/DynamicQuery1-1.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-54942\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/DynamicQuery1-1.jpg\" alt=\"\" width=\"1200\" height=\"493\" \/><\/a><br \/>\nWe can see that the query runs very fast, 7ms, it uses index seek on Posts table with our non-clustered index, the Posts clustered index for the lookup to find the others needed columns and do an index seek also on Users table with the clustered index of this table.<br \/>\nNothing special to tell here.<br \/>\nLets run the same Stored Procedure with ParentId equals to 0 and check the result:<br \/>\n<a href=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/DynamicQuery2-3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-54949\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/DynamicQuery2-3.jpg\" alt=\"\" width=\"1200\" height=\"471\" \/><\/a><br \/>\nThe result is not exactly the same&#8230; The execution time increases dramatically with more than 11 minutes.<br \/>\nThe query retrieves more than 6 Millions rows instead of 518&#8230; it takes a lot of time processing these rows for the fourth operators.<br \/>\nHere we have issues with parameter sniffing where a plan is very good for a small number of rows but bad for a large number of rows.<br \/>\nThe first question is &#8220;can we fix the key lookup?&#8221;, here we want to retrieve all columns of the Posts table so we will not create an index which will cover the start of a query.<br \/>\nIf we remove again the plans cached, execute the second query first (with ParentID = 0) and after the one with ParentId equals 184618 we have a completely different results:<br \/>\n<a href=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/DynamicQuery3.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-54948\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/DynamicQuery3.jpg\" alt=\"\" width=\"1500\" height=\"760\" \/><\/a><br \/>\nExecution for ParentId equals 0 is now really fast compare to the previous one and the one with ParentId equals 184618 is still acceptable.<br \/>\nBut the plan has changed and if the query is executed in the bad order performance problem will come back.<br \/>\nA way to solve this issue would be to add the hint OPTION(RECOMPILE) in the Stored Procedure to RECOMPILE our query and recreate a fresh query plan each time the SP is executed.<br \/>\nBut Erik showed us that doing recompilation each time the Stored Procedure is executed can used CPU and it&#8217;s not where we want to use our CPU!<br \/>\nTo fix our problem we can try to create a selective index on the Posts table, put the column ParentID first and have a look of that happens. But if we check the distribution of the data in the Posts table:<br \/>\n<a href=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/DynamicQuery4.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-54959\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/DynamicQuery4.jpg\" alt=\"\" width=\"427\" height=\"753\" \/><\/a><br \/>\nThe selectivity of our index will be more than poor for 99% of the records&#8230;<br \/>\nTo avoid this recompilation problem and to boost our SP the best way is to use Dynamic SQL.<br \/>\nWe will write a Dynamic SQL which will force the recompilation if we execute our Stored Procedure for ParentId equals 0:<br \/>\n<code>CREATE OR ALTER PROCEDURE<br \/>\ndbo.MySP<br \/>\n(<br \/>\n@ParentId int<br \/>\n)<br \/>\nAS<br \/>\nBEGIN<br \/>\nSET NOCOUNT, XACT_ABORT ON;<\/p>\n<p>DECLARE<br \/>\n@sql nvarchar(MAX) = N'';<\/p>\n<p>SET @sql += N'<br \/>\nSELECT TOP (10)<br \/>\nu.DisplayName,<br \/>\np.*<br \/>\n\/*dbo.MySP*\/<br \/>\nFROM dbo.Posts AS p<br \/>\nJOIN dbo.Users AS u<br \/>\nON p.OwnerUserId = u.Id<br \/>\nWHERE p.ParentId = @iParentId<br \/>\nORDER BY u.Reputation DESC';<br \/>\nIF @ParentId = 0<br \/>\nBEGIN<br \/>\nSET @sql += N'<br \/>\nOPTION(RECOMPILE);';<br \/>\nEND;<br \/>\nELSE<br \/>\nBEGIN<br \/>\n--We're so tidy.<br \/>\nSET @sql += N';';<br \/>\nEND;<\/p>\n<p>EXEC sys.sp_executesql<br \/>\n@sql,<br \/>\nN'@iParentId int',<br \/>\n@iParentId = @ParentId;<\/p>\n<p>END;<br \/>\nGO<\/code><br \/>\nIn this code we check if the parameter @ParentId of the Stored Procedure is 0 and in this case we force a recompilation. So we will have a complete new plan for the value 0 but not for the other ones where the number of records is really low and where we don&#8217;t have a chance to have a bad plan.<br \/>\nIf we execute our 2 queries:<br \/>\n<a href=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/DynamicQuery5.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-54960\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/DynamicQuery5.jpg\" alt=\"\" width=\"1595\" height=\"987\" \/><\/a><br \/>\nNow both plans are perfectly fast, we can observe on the second one that we have our option recompile which generated an optimized parallelized plan for ParentId equals 0.<br \/>\nThis kind of pattern is efficient in our case but will not be if we don&#8217;t know the values which cause problem&#8230;<br \/>\nErik gave us another way to create a dynamic SQL script which will solve the problem when problem values are unknown.<br \/>\nThe goal here is to create a new plan for any query that comes in but without recompilation.<br \/>\nFor that Erik don&#8217;t use anymore the RECOMPILE hint but will use the OPTIMIZE FOR hint with a funny value which will be replace for the execution by the value of the ParentId parameter.<br \/>\nAs SQL Server caches execution plan based on the query text, a new plan will be create for each value of the parameter.<br \/>\n<code>CREATE OR ALTER PROCEDURE<br \/>\ndbo.mySP<br \/>\n(<br \/>\n@ParentId int<br \/>\n)<br \/>\nAS<br \/>\nSET NOCOUNT, XACT_ABORT ON;<br \/>\nBEGIN<\/p>\n<p>DECLARE<br \/>\n@sql nvarchar(MAX) = N'';<\/p>\n<p>SET @sql += N'<br \/>\nSELECT TOP (10)<br \/>\nu.DisplayName,<br \/>\np.*<br \/>\n\/*dbo.OptionalRecompile*\/<br \/>\nFROM dbo.Posts AS p<br \/>\nJOIN dbo.Users AS u<br \/>\nON p.OwnerUserId = u.Id<br \/>\nWHERE p.ParentId = @iParentId<br \/>\nORDER BY u.Reputation DESC<br \/>\nOPTION(OPTIMIZE FOR(@iParentId = [@@@]));<br \/>\n' + NCHAR(10);<\/p>\n<p>SET @sql =<br \/>\nREPLACE<br \/>\n(<br \/>\n@sql,<br \/>\nN'[@@@]',<br \/>\n@ParentId<br \/>\n);<\/p>\n<p>--to print the script<br \/>\nRAISERROR(@sql, 0, 1) WITH NOWAIT;<\/p>\n<p>SET STATISTICS XML ON;<\/p>\n<p>EXECUTE sys.sp_executesql<br \/>\n@sql,<br \/>\nN'@iParentId int',<br \/>\n@ParentId;<\/p>\n<p>SET STATISTICS XML OFF;<\/p>\n<p>END;<br \/>\nGO<\/code><br \/>\nIf we execute our Stored Procedure now with our two values:<br \/>\n<a href=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/DynamicQuery6.jpg\"><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-54961\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/DynamicQuery6.jpg\" alt=\"\" width=\"1901\" height=\"991\" \/><\/a><br \/>\nWe have a specific plan for each value, we can see our hint OPTIMIZE FOR (@ParentID = with the value of our parameter. Plans are the same than before with the RECOMPILE hint but this dynamic SQL is more generic and can be used with unknown problem values.<br \/>\nWe can verify that cached plan is used for each parameter values and that we don&#8217;t have a new plan after each execution (like with recompile) with the sp_BlitzCache Stored Procedure from <a href=\"https:\/\/www.brentozar.com\/\">Brent Ozar<\/a>:<br \/>\n<a href=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/DynamicQuery7.jpg\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/DynamicQuery7.jpg\" alt=\"\" width=\"1862\" height=\"584\" class=\"alignnone size-full wp-image-54976\" \/><\/a><br \/>\nThat did the trick!<\/p>\n<p>I hope this tip can help you \ud83d\ude09<br \/>\nI would like to warmly thanks <a href=\"https:\/\/www.erikdarlingdata.com\/\">Erik Darling<\/a> for those tips and the excellent training session (The Professional Performance Tuning Blueprint) he did during the SQLBits 2022.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Since Tuesday, I have the chance to follow the SQLBits 2022 conference. Sadly, I have to do it remotely this year but I promise next time I will come in-person \ud83d\ude09 SQLBits is one of the largest SQL Server conference in the world with some famous speakers like Brent Ozar, Bob Ward, Erik Darling&#8230; In [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[229,198,99],"tags":[2515,741,644,51,2514],"type_dbi":[],"class_list":["post-17151","post","type-post","status-publish","format-standard","hentry","category-database-administration-monitoring","category-database-management","category-sql-server","tag-dynamic-sql","tag-parameter-sniffing","tag-performance-tuning","tag-sql-server","tag-sqlbits"],"acf":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO Premium plugin v27.2 (Yoast SEO v27.2) - https:\/\/yoast.com\/product\/yoast-seo-premium-wordpress\/ -->\n<title>How to solve Stored Procedure parameter sniffing problems - dbi Blog<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"How to solve Stored Procedure parameter sniffing problems\" \/>\n<meta property=\"og:description\" content=\"Since Tuesday, I have the chance to follow the SQLBits 2022 conference. Sadly, I have to do it remotely this year but I promise next time I will come in-person \ud83d\ude09 SQLBits is one of the largest SQL Server conference in the world with some famous speakers like Brent Ozar, Bob Ward, Erik Darling&#8230; In [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2022-03-11T11:35:41+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2022-05-31T14:37:54+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/Flag_sqlbits_2022.jpg\" \/>\n<meta name=\"author\" content=\"St\u00e9phane Savorgnano\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"St\u00e9phane Savorgnano\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"6 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/\"},\"author\":{\"name\":\"St\u00e9phane Savorgnano\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/b6bce7d75118b35bdb3b439ad6a9ca3c\"},\"headline\":\"How to solve Stored Procedure parameter sniffing problems\",\"datePublished\":\"2022-03-11T11:35:41+00:00\",\"dateModified\":\"2022-05-31T14:37:54+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/\"},\"wordCount\":928,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/Flag_sqlbits_2022.jpg\",\"keywords\":[\"Dynamic SQL\",\"parameter sniffing\",\"Performance Tuning\",\"SQL Server\",\"SQLBits\"],\"articleSection\":[\"Database Administration &amp; Monitoring\",\"Database management\",\"SQL Server\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/\",\"name\":\"How to solve Stored Procedure parameter sniffing problems - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/Flag_sqlbits_2022.jpg\",\"datePublished\":\"2022-03-11T11:35:41+00:00\",\"dateModified\":\"2022-05-31T14:37:54+00:00\",\"author\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/b6bce7d75118b35bdb3b439ad6a9ca3c\"},\"breadcrumb\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/#primaryimage\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/Flag_sqlbits_2022.jpg\",\"contentUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/Flag_sqlbits_2022.jpg\"},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/www.dbi-services.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"How to solve Stored Procedure parameter sniffing problems\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/\",\"name\":\"dbi Blog\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Person\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/b6bce7d75118b35bdb3b439ad6a9ca3c\",\"name\":\"St\u00e9phane Savorgnano\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/88d2a790f775c52c1012ec644d883431da758f2cbcfc16067ade04d2ef625ef5?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/88d2a790f775c52c1012ec644d883431da758f2cbcfc16067ade04d2ef625ef5?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/88d2a790f775c52c1012ec644d883431da758f2cbcfc16067ade04d2ef625ef5?s=96&d=mm&r=g\",\"caption\":\"St\u00e9phane Savorgnano\"},\"description\":\"St\u00e9phane Savorgnano has more than fifteen years of experience in Microsoft software development and in SQL Server database solutions. He is specialized in SQL Server installation, performance analysis, best practices, etc. St\u00e9phane Savorgnano is Microsoft Certified Solutions Associate (MCSA) and\u00a0Microsoft Certified Solutions Expert (MCSE) for SQL Server 2012. He is also Microsoft Certified Technology Specialist (MCTS) and Microsoft Certified IT Professional (MCITP) for SQL Server 2008 as well as ITIL Foundation V3 certified. Prior to joining dbi services, he was software engineer at Ciba Specialty Chemicals in Basel. St\u00e9phane Savorgnano holds a Master of Informatics from Mulhouse University (F). His branch-related experience covers Banking \/ Financial Services, Chemicals &amp; Pharmaceuticals, etc.\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/author\/stephane-savorgnano\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"How to solve Stored Procedure parameter sniffing problems - dbi Blog","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/","og_locale":"en_US","og_type":"article","og_title":"How to solve Stored Procedure parameter sniffing problems","og_description":"Since Tuesday, I have the chance to follow the SQLBits 2022 conference. Sadly, I have to do it remotely this year but I promise next time I will come in-person \ud83d\ude09 SQLBits is one of the largest SQL Server conference in the world with some famous speakers like Brent Ozar, Bob Ward, Erik Darling&#8230; In [&hellip;]","og_url":"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/","og_site_name":"dbi Blog","article_published_time":"2022-03-11T11:35:41+00:00","article_modified_time":"2022-05-31T14:37:54+00:00","og_image":[{"url":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/Flag_sqlbits_2022.jpg","type":"","width":"","height":""}],"author":"St\u00e9phane Savorgnano","twitter_card":"summary_large_image","twitter_misc":{"Written by":"St\u00e9phane Savorgnano","Est. reading time":"6 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/"},"author":{"name":"St\u00e9phane Savorgnano","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/b6bce7d75118b35bdb3b439ad6a9ca3c"},"headline":"How to solve Stored Procedure parameter sniffing problems","datePublished":"2022-03-11T11:35:41+00:00","dateModified":"2022-05-31T14:37:54+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/"},"wordCount":928,"commentCount":0,"image":{"@id":"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/#primaryimage"},"thumbnailUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/Flag_sqlbits_2022.jpg","keywords":["Dynamic SQL","parameter sniffing","Performance Tuning","SQL Server","SQLBits"],"articleSection":["Database Administration &amp; Monitoring","Database management","SQL Server"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/","url":"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/","name":"How to solve Stored Procedure parameter sniffing problems - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/#primaryimage"},"image":{"@id":"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/#primaryimage"},"thumbnailUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/Flag_sqlbits_2022.jpg","datePublished":"2022-03-11T11:35:41+00:00","dateModified":"2022-05-31T14:37:54+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/b6bce7d75118b35bdb3b439ad6a9ca3c"},"breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/#primaryimage","url":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/Flag_sqlbits_2022.jpg","contentUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/03\/Flag_sqlbits_2022.jpg"},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/how-to-solve-stored-procedure-parameter-sniffing-problems\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"How to solve Stored Procedure parameter sniffing problems"}]},{"@type":"WebSite","@id":"https:\/\/www.dbi-services.com\/blog\/#website","url":"https:\/\/www.dbi-services.com\/blog\/","name":"dbi Blog","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.dbi-services.com\/blog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Person","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/b6bce7d75118b35bdb3b439ad6a9ca3c","name":"St\u00e9phane Savorgnano","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/88d2a790f775c52c1012ec644d883431da758f2cbcfc16067ade04d2ef625ef5?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/88d2a790f775c52c1012ec644d883431da758f2cbcfc16067ade04d2ef625ef5?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/88d2a790f775c52c1012ec644d883431da758f2cbcfc16067ade04d2ef625ef5?s=96&d=mm&r=g","caption":"St\u00e9phane Savorgnano"},"description":"St\u00e9phane Savorgnano has more than fifteen years of experience in Microsoft software development and in SQL Server database solutions. He is specialized in SQL Server installation, performance analysis, best practices, etc. St\u00e9phane Savorgnano is Microsoft Certified Solutions Associate (MCSA) and\u00a0Microsoft Certified Solutions Expert (MCSE) for SQL Server 2012. He is also Microsoft Certified Technology Specialist (MCTS) and Microsoft Certified IT Professional (MCITP) for SQL Server 2008 as well as ITIL Foundation V3 certified. Prior to joining dbi services, he was software engineer at Ciba Specialty Chemicals in Basel. St\u00e9phane Savorgnano holds a Master of Informatics from Mulhouse University (F). His branch-related experience covers Banking \/ Financial Services, Chemicals &amp; Pharmaceuticals, etc.","url":"https:\/\/www.dbi-services.com\/blog\/author\/stephane-savorgnano\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/17151","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=17151"}],"version-history":[{"count":1,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/17151\/revisions"}],"predecessor-version":[{"id":17152,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/17151\/revisions\/17152"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=17151"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=17151"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=17151"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=17151"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}