{"id":82923,"date":"2019-01-12T20:42:00","date_gmt":"2019-01-12T20:42:00","guid":{"rendered":"https:\/\/www.red-gate.com\/simple-talk\/?p=82923"},"modified":"2019-01-12T20:42:00","modified_gmt":"2019-01-12T20:42:00","slug":"a-real-parameterization-problem-with-a-plus","status":"publish","type":"post","link":"https:\/\/www.red-gate.com\/simple-talk\/blogs\/a-real-parameterization-problem-with-a-plus\/","title":{"rendered":"A real parameterization problem with a plus"},"content":{"rendered":"<p>A few weeks ago I faced this problem: One query on my application was (fortunately in the development environment) was facing a very bad execution time.<\/p>\n<p>Since the query was generated by entity framework, I used <strong>SQL Profiler<\/strong> to capture the query with all its parameters and execute in <strong>SSMS<\/strong>. The query was created using <strong>sp_executesql<\/strong>, this stored procedure is used to force a query to be parameterized.<\/p>\n<p>The surprise: The execution was quite well on <strong>SSMS<\/strong>. The query was the same, same server, same data, same parameters and still, the execution time was completely different. I cleared the cache many times, tried again the application, then <strong>SSMS<\/strong> and still, completely different execution times on both. The execution should be the same, why in hell the plan created when the application sends the query would be different than the plan created when <strong>SSMS<\/strong> sends the query?<\/p>\n<p>Ok, the plans shouldn&#8217;t be different, but let&#8217;s check anyway. We can recover the query plans from the cache:<\/p>\n<div><span style=\"font-family: Courier New;font-size: 10pt\"> <span style=\"color: blue\">select<\/span>\u00a0<span style=\"color: silver\">*<\/span>\u00a0<span style=\"color: blue\">from<\/span> <br \/>\n<span style=\"color: maroon\">sys<\/span><span style=\"color: silver\">.<\/span><span style=\"color: maroon\">dm_exec_query_stats<\/span>\u00a0<span style=\"color: maroon\">qs<\/span> <br \/>\n<span style=\"color: blue\">cross<\/span>\u00a0<span style=\"color: maroon\">apply<\/span>\u00a0<span style=\"color: maroon\">sys<\/span><span style=\"color: silver\">.<\/span><span style=\"color: #ff0080\"><b>dm_exec_sql_text<\/b><\/span><span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">qs<\/span><span style=\"color: silver\">.<\/span><span style=\"color: maroon\">sql_handle<\/span><span style=\"color: maroon\">)<\/span> <br \/>\n<span style=\"color: blue\">cross<\/span>\u00a0<span style=\"color: maroon\">apply<\/span>\u00a0<span style=\"color: maroon\">sys<\/span><span style=\"color: silver\">.<\/span><span style=\"color: #ff0080\"><b>dm_exec_query_plan<\/b><\/span><span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">qs<\/span><span style=\"color: silver\">.<\/span><span style=\"color: maroon\">plan_handle<\/span><span style=\"color: maroon\">)<\/span> <br \/>\n<span style=\"color: blue\">order<\/span>\u00a0<span style=\"color: blue\">by<\/span>\u00a0<span style=\"color: maroon\">total_elapsed_time<\/span>\u00a0<span style=\"color: blue\">desc<\/span> <\/span><\/div>\n<p>&nbsp;<\/p>\n<p>This query will retrieve all the plans from the cache. A few more filters over the query text and we can find the plans related to the query we are looking for. Surprise again: The same query has two different plans.<\/p>\n<p>How do I know it&#8217;s the same query? Couldn&#8217;t the text be different? Simple: The <strong>sql_handle<\/strong> was absolutely the same. It was the same query. Why different plans?<\/p>\n<p>My first thought was the user. When a table name is not fully qualified (&lt;schema&gt;.&lt;table&gt;), the query plan can be fixed for a specific user and this could be a problem.<\/p>\n<p>Using the query plan handles from both plans of the query and the query below we can identify if the plans are linked to a specific user or not.<\/p>\n<div><span style=\"font-family: Courier New;font-size: 10pt\"> <span style=\"color: blue\">with<\/span>\u00a0<span style=\"color: maroon\">qry<\/span>\u00a0<span style=\"color: blue\">as<\/span> <br \/>\n<span style=\"color: maroon\">(<\/span><span style=\"color: blue\">select<\/span>\u00a0<span style=\"color: maroon\">refcounts<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">usecounts<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">size_in_bytes<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: maroon\">cacheobjtype<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">objtype<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: maroon\">attribute<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">value<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">plan_handle<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: blue\">from<\/span>\u00a0<span style=\"color: maroon\">sys<\/span><span style=\"color: silver\">.<\/span><span style=\"color: maroon\">dm_exec_cached_plans<\/span>\u00a0<span style=\"color: maroon\">ecp<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: blue\">outer<\/span>\u00a0<span style=\"color: maroon\">apply<\/span>\u00a0<span style=\"color: maroon\">sys<\/span><span style=\"color: silver\">.<\/span><span style=\"color: #ff0080\"><b>dm_exec_plan_attributes<\/b><\/span><span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">ecp<\/span><span style=\"color: silver\">.<\/span><span style=\"color: maroon\">plan_handle<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: maroon\">epa<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: blue\">where<\/span>\u00a0<span style=\"color: maroon\">epa<\/span><span style=\"color: silver\">.<\/span><span style=\"color: maroon\">attribute<\/span><span style=\"color: silver\">=<\/span><span style=\"color: red\">&#8216;user_id&#8217;<\/span><span style=\"color: maroon\">)<\/span> <br \/>\n<span style=\"color: blue\">select<\/span>\u00a0<span style=\"color: maroon\">refcounts<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">usecounts<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">size_in_bytes<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">cacheobjtype<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">objtype<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">attribute<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">value<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: maroon\">objectid<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">[text]<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">[dbid]<\/span> <br \/>\n<span style=\"color: blue\">from<\/span>\u00a0<span style=\"color: maroon\">qry<\/span> <br \/>\n<span style=\"color: blue\">cross<\/span>\u00a0<span style=\"color: maroon\">apply<\/span>\u00a0<span style=\"color: maroon\">sys<\/span><span style=\"color: silver\">.<\/span><span style=\"color: #ff0080\"><b>dm_exec_sql_text<\/b><\/span><span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">qry<\/span><span style=\"color: silver\">.<\/span><span style=\"color: maroon\">plan_handle<\/span><span style=\"color: maroon\">)<\/span> <br \/>\n\u00a0\u00a0\u00a0<span style=\"color: blue\">where<\/span>\u00a0<span style=\"color: maroon\">dbid<\/span><span style=\"color: silver\">=<\/span><span style=\"color: maroon\">db_id<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: silver\">&lt;<\/span><span style=\"color: maroon\">databaseName<\/span><span style=\"color: silver\">&gt;<\/span><span style=\"color: maroon\">)<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">and<\/span>\u00a0<span style=\"color: maroon\">plan_handle<\/span>\u00a0<span style=\"color: blue\">in<\/span>\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: silver\">&lt;<\/span><span style=\"color: maroon\">planhandle1<\/span><span style=\"color: silver\">&gt;<\/span><span style=\"color: silver\">,<\/span><span style=\"color: silver\">&lt;<\/span><span style=\"color: maroon\">planhandle2<\/span><span style=\"color: silver\">&gt;<\/span><span style=\"color: maroon\">)<\/span> <\/span><\/div>\n<p>&nbsp;<\/p>\n<p>No solution: both plans with a -2 value for the <strong>user_id<\/strong>, so both can be used by any user. Oh, wait: <strong>user_id<\/strong> is a plan attribute, what about the other plan attributes? We just need to remove the where clause on this previous query to check:<\/p>\n<div><span style=\"font-family: Courier New;font-size: 10pt\"> <span style=\"color: blue\">with<\/span>\u00a0<span style=\"color: maroon\">qry<\/span>\u00a0<span style=\"color: blue\">as<\/span> <br \/>\n<span style=\"color: maroon\">(<\/span><span style=\"color: blue\">select<\/span>\u00a0<span style=\"color: maroon\">refcounts<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">usecounts<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">size_in_bytes<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: maroon\">cacheobjtype<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">objtype<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: maroon\">attribute<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">value<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">plan_handle<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: blue\">from<\/span>\u00a0<span style=\"color: maroon\">sys<\/span><span style=\"color: silver\">.<\/span><span style=\"color: maroon\">dm_exec_cached_plans<\/span>\u00a0<span style=\"color: maroon\">ecp<\/span> <br \/>\n\u00a0\u00a0<span style=\"color: blue\">outer<\/span>\u00a0<span style=\"color: maroon\">apply<\/span>\u00a0<span style=\"color: maroon\">sys<\/span><span style=\"color: silver\">.<\/span><span style=\"color: #ff0080\"><b>dm_exec_plan_attributes<\/b><\/span><span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">ecp<\/span><span style=\"color: silver\">.<\/span><span style=\"color: maroon\">plan_handle<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: maroon\">epa<\/span><span style=\"color: maroon\">)<\/span> <br \/>\n<span style=\"color: blue\">select<\/span>\u00a0<span style=\"color: maroon\">refcounts<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">usecounts<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">size_in_bytes<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">cacheobjtype<\/span><span style=\"color: silver\">,<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: maroon\">objtype<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">attribute<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">value<\/span><span style=\"color: silver\">,<\/span>\u00a0<span style=\"color: maroon\">objectid<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">[text]<\/span><span style=\"color: silver\">,<\/span><span style=\"color: maroon\">[dbid]<\/span> <br \/>\n<span style=\"color: blue\">from<\/span>\u00a0<span style=\"color: maroon\">qry<\/span> <br \/>\n<span style=\"color: blue\">cross<\/span>\u00a0<span style=\"color: maroon\">apply<\/span>\u00a0<span style=\"color: maroon\">sys<\/span><span style=\"color: silver\">.<\/span><span style=\"color: #ff0080\"><b>dm_exec_sql_text<\/b><\/span><span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">qry<\/span><span style=\"color: silver\">.<\/span><span style=\"color: maroon\">plan_handle<\/span><span style=\"color: maroon\">)<\/span> <br \/>\n\u00a0\u00a0\u00a0<span style=\"color: blue\">where<\/span>\u00a0<span style=\"color: maroon\">dbid<\/span><span style=\"color: silver\">=<\/span><span style=\"color: maroon\">db_id<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: silver\">&lt;<\/span><span style=\"color: maroon\">databaseName<\/span><span style=\"color: silver\">&gt;<\/span><span style=\"color: maroon\">)<\/span> <br \/>\n\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0<span style=\"color: blue\">and<\/span>\u00a0<span style=\"color: maroon\">plan_handle<\/span>\u00a0<span style=\"color: blue\">in<\/span>\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: silver\">&lt;<\/span><span style=\"color: maroon\">planhandle1<\/span><span style=\"color: silver\">&gt;<\/span><span style=\"color: silver\">,<\/span><span style=\"color: silver\">&lt;<\/span><span style=\"color: maroon\">planhandle2<\/span><span style=\"color: silver\">&gt;<\/span><span style=\"color: maroon\">)<\/span> <\/span><\/div>\n<p>&nbsp;<\/p>\n<p>That&#8217;s it! Comparing all the attributes, one of them is different between both plans: set_options.<\/p>\n<p>This attribute, set_options, is expressed with a numeric value which needs some bitwise operations to be parsed. Here a script to parse the set_options value, you can read it in details <a href=\"https:\/\/daleburnett.com\/2011\/10\/parsing-the-set-options-in-sys-dm_exec_plan_attributes\/\">here<\/a><\/p>\n<div><span style=\"font-family: Courier New;font-size: 10pt\"> <span style=\"color: blue\">declare<\/span>\u00a0<span style=\"color: #8000ff\">@set_options<\/span>\u00a0<span style=\"color: black\"><i>int<\/i><\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: black\">251<br \/>\n<\/span><span style=\"color: blue\">if<\/span>\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">1<\/span>\u00a0<span style=\"color: silver\">&amp;<\/span>\u00a0<span style=\"color: #8000ff\">@set_options<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: black\">1<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">print<\/span>\u00a0<span style=\"color: red\">&#8216;ANSI_PADDING&#8217;<br \/>\n<\/span><span style=\"color: blue\">if<\/span>\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">4<\/span>\u00a0<span style=\"color: silver\">&amp;<\/span>\u00a0<span style=\"color: #8000ff\">@set_options<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: black\">4<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">print<\/span>\u00a0<span style=\"color: red\">&#8216;FORCEPLAN&#8217;<br \/>\n<\/span><span style=\"color: blue\">if<\/span>\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">8<\/span>\u00a0<span style=\"color: silver\">&amp;<\/span>\u00a0<span style=\"color: #8000ff\">@set_options<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: black\">8<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">print<\/span>\u00a0<span style=\"color: red\">&#8216;CONCAT_NULL_YIELDS_NULL&#8217;<br \/>\n<\/span><span style=\"color: blue\">if<\/span>\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">16<\/span>\u00a0<span style=\"color: silver\">&amp;<\/span>\u00a0<span style=\"color: #8000ff\">@set_options<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: black\">16<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">print<\/span>\u00a0<span style=\"color: red\">&#8216;ANSI_WARNINGS&#8217;<br \/>\n<\/span><span style=\"color: blue\">if<\/span>\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">32<\/span>\u00a0<span style=\"color: silver\">&amp;<\/span>\u00a0<span style=\"color: #8000ff\">@set_options<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: black\">32<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">print<\/span>\u00a0<span style=\"color: red\">&#8216;ANSI_NULLS&#8217;<br \/>\n<\/span><span style=\"color: blue\">if<\/span>\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">64<\/span>\u00a0<span style=\"color: silver\">&amp;<\/span>\u00a0<span style=\"color: #8000ff\">@set_options<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: black\">64<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">print<\/span>\u00a0<span style=\"color: red\">&#8216;QUOTED_IDENTIFIER&#8217;<br \/>\n<\/span><span style=\"color: blue\">if<\/span>\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">128<\/span>\u00a0<span style=\"color: silver\">&amp;<\/span>\u00a0<span style=\"color: #8000ff\">@set_options<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: black\">128<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">print<\/span>\u00a0<span style=\"color: red\">&#8216;ANSI_NULL_DFLT_ON&#8217;<br \/>\n<\/span><span style=\"color: blue\">if<\/span>\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">256<\/span>\u00a0<span style=\"color: silver\">&amp;<\/span>\u00a0<span style=\"color: #8000ff\">@set_options<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: black\">256<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">print<\/span>\u00a0<span style=\"color: red\">&#8216;ANSI_NULL_DFLT_OFF&#8217;<br \/>\n<\/span><span style=\"color: blue\">if<\/span>\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">512<\/span>\u00a0<span style=\"color: silver\">&amp;<\/span>\u00a0<span style=\"color: #8000ff\">@set_options<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: black\">512<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">print<\/span>\u00a0<span style=\"color: red\">&#8216;NoBrowseTable&#8217;<br \/>\n<\/span><span style=\"color: blue\">if<\/span>\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">4096<\/span>\u00a0<span style=\"color: silver\">&amp;<\/span>\u00a0<span style=\"color: #8000ff\">@set_options<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: black\">4096<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">print<\/span>\u00a0<span style=\"color: red\">&#8216;ARITH_ABORT&#8217;<br \/>\n<\/span><span style=\"color: blue\">if<\/span>\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">8192<\/span>\u00a0<span style=\"color: silver\">&amp;<\/span>\u00a0<span style=\"color: #8000ff\">@set_options<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: black\">8192<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">print<\/span>\u00a0<span style=\"color: red\">&#8216;NUMERIC_ROUNDABORT&#8217;<br \/>\n<\/span><span style=\"color: blue\">if<\/span>\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">16384<\/span>\u00a0<span style=\"color: silver\">&amp;<\/span>\u00a0<span style=\"color: #8000ff\">@set_options<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: black\">16384<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">print<\/span>\u00a0<span style=\"color: red\">&#8216;DATEFIRST&#8217;<br \/>\n<\/span><span style=\"color: blue\">if<\/span>\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">32768<\/span>\u00a0<span style=\"color: silver\">&amp;<\/span>\u00a0<span style=\"color: #8000ff\">@set_options<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: black\">32768<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">print<\/span>\u00a0<span style=\"color: red\">&#8216;DATEFORMAT&#8217;<br \/>\n<\/span><span style=\"color: blue\">if<\/span>\u00a0<span style=\"color: maroon\">(<\/span><span style=\"color: maroon\">(<\/span><span style=\"color: black\">65536<\/span>\u00a0<span style=\"color: silver\">&amp;<\/span>\u00a0<span style=\"color: #8000ff\">@set_options<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: silver\">=<\/span>\u00a0<span style=\"color: black\">65536<\/span><span style=\"color: maroon\">)<\/span>\u00a0<span style=\"color: blue\">print<\/span>\u00a0<span style=\"color: red\">&#8216;LanguageID&#8217;<\/span> <\/span><\/div>\n<p>&nbsp;<\/p>\n<p>Finally, the answer: <strong>SSMS<\/strong> had one single option more than the connections made by the application. <strong>ARITH_ABORT<\/strong> changes the <strong>SQL Server<\/strong> behaviour when a math error, such as overflow or division by zero, happens. Should <strong>SQL Server<\/strong> return null or raises an error?<\/p>\n<p>The option itself doesn&#8217;t affect the query in anything, however, since SSMS has a different set option enabled, SQL Server refuses to re-use the query plan created for the application and compile a new one. This totally hides the source of the problem.<\/p>\n<p>After discovering the logic behind the different query plans, identifying the problem was quite easy. The query was parameterized, it was using sp_executesql. The application is a web application hosted inside sharepoint. The first time the page loads, it loads a report with only a quarter for 2018, a small set of data. However, by doing this the query is sent to the server and a query plan is stored on the cache, compiled for this set of parameters and data. When the user changes the parameters, requesting a report from 2014 to 2018, the query returns a completely different amount of data and the plan works very badly for it.<\/p>\n<p>In summary, it&#8217;s a parameterization problem, when the query needs different plans for different parameter values. I wrote about the problem, how to identify and solve, you can read here. I was capturing the text and parameters of the query with problems and when executing in SSMS, due to the different set options, it was always recompiled and a new (and good) plan was created, hiding the parameterization problem.<\/p>\n<p>The solution for the parameterization problem is to use a query hint. There are two options:<\/p>\n<p style=\"padding-left: 30px\"><strong>Recompile<\/strong>: This hint will ensure the query will always be recompiled, never using a plan from the cache and ensuring it always has a good plan for the current parameters.<\/p>\n<p style=\"padding-left: 30px\"><strong>Optimize for Unknown<\/strong>: The parameterization problem happens because the query is optimized for the parameter values it&#8217;s using when it first arrives on the server and this optimization may not work for other parameter values. The Optimize for Unknow hint optimize the query for any parameter value, disregarding the values used on the first execution, so it&#8217;s more probably the resulting plan will work well enough for any parameter and the query will not need to be recompiled on each execution. On the other hand, some parameters that could have a better plan will also be using this &#8220;generic&#8221; plan.<\/p>\n<p>However, the query was generated by entity framework and entity framework doesn&#8217;t support query hints. How to solve this problem? Some time ago I wrote an article about how to create a library to support these query hints with entity framework. You can read the details <a href=\"https:\/\/www.red-gate.com\/simple-talk\/dotnet\/net-development\/using-sql-server-query-hints-entity-framework\/\">here<\/a> and get a copy of the library <a href=\"https:\/\/github.com\/DennesTorres\/HintsInterceptor\">here<\/a>.<\/p>\n<p>Finally, problem solved after an interesting catch be found hiding the real problem.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>A few weeks ago I faced this problem: One query on my application was (fortunately in the development environment) was facing a very bad execution time. Since the query was generated by entity framework, I used SQL Profiler to capture the query with all its parameters and execute in SSMS. The query was created using&#8230;&hellip;<\/p>\n","protected":false},"author":50808,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[2],"tags":[4170,5993],"coauthors":[6810],"class_list":["post-82923","post","type-post","status-publish","format-standard","hentry","category-blogs","tag-database-administration","tag-query-plans"],"acf":[],"_links":{"self":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82923","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/users\/50808"}],"replies":[{"embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/comments?post=82923"}],"version-history":[{"count":2,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82923\/revisions"}],"predecessor-version":[{"id":82925,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/posts\/82923\/revisions\/82925"}],"wp:attachment":[{"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/media?parent=82923"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/categories?post=82923"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/tags?post=82923"},{"taxonomy":"author","embeddable":true,"href":"https:\/\/www.red-gate.com\/simple-talk\/wp-json\/wp\/v2\/coauthors?post=82923"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}