{"id":4453,"date":"2015-03-18T15:31:48","date_gmt":"2015-03-18T14:31:48","guid":{"rendered":"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/"},"modified":"2015-03-18T15:31:48","modified_gmt":"2015-03-18T14:31:48","slug":"a-funny-story-with-recompile-hint-at-statement-level","status":"publish","type":"post","link":"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/","title":{"rendered":"A funny story with recompile hint at statement level"},"content":{"rendered":"<p>Last night, I had an interesting discussion with one of my MVP French friend that faces a weird situation where a query that uses a statement level RECOMPILE hint produces an execution plan that disappointed him. He told me we can simulate the same situation by using the AdventureWorks database and Sales.SalesOrderHeader table.<\/p>\n<p>First, we have to add a nonclustered index on the orderdate column as follows:<\/p>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">CREATE<span style=\"font-size: 9.5pt;font-family: Consolas\"> <span style=\"color: blue\">NONCLUSTERED <span style=\"color: blue\">INDEX [idx_sales_salesorderheader_orderdate] <span style=\"color: blue\">ON [Sales]<span style=\"color: gray\">.[SalesOrderHeader]<\/span><\/span><\/span><\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: gray\">(<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas\">\u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 [OrderDate] <span style=\"color: blue\">ASC<\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: gray\">)<\/span><\/div>\n<p>&nbsp;<\/p>\n<p>Now, let me show you the query. In fact we compare two queries and two potential behaviours. Indeed, the first query will use a local variable and an inequality operator while the second query will be pretty the same except we add the recompile hint option.<\/p>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">declare<span style=\"font-size: 9.5pt;font-family: Consolas\"> @date <span style=\"color: blue\">datetime <span style=\"color: gray\">= <span style=\"color: fuchsia\">getdate<span style=\"color: gray\">()<\/span><\/span><\/span><\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas\">\u00a0<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">select<span style=\"font-size: 9.5pt;font-family: Consolas\"> <span style=\"color: blue\">top 1 SalesOrderID <\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">from<span style=\"font-size: 9.5pt;font-family: Consolas\"> sales<span style=\"color: gray\">.salesorderheader <\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">where<span style=\"font-size: 9.5pt;font-family: Consolas\"> orderdate <span style=\"color: gray\">&gt;= @date <\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">order<span style=\"font-size: 9.5pt;font-family: Consolas\"> <span style=\"color: blue\">by SalesOrderID<\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">go<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas\">\u00a0<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">declare<span style=\"font-size: 9.5pt;font-family: Consolas\"> @date <span style=\"color: blue\">datetime <span style=\"color: gray\">= <span style=\"color: fuchsia\">getdate<span style=\"color: gray\">()<\/span><\/span><\/span><\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas\">\u00a0<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">select<span style=\"font-size: 9.5pt;font-family: Consolas\"> <span style=\"color: blue\">top 1 SalesOrderID <\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">from<span style=\"font-size: 9.5pt;font-family: Consolas\"> sales<span style=\"color: gray\">.salesorderheader <\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">where<span style=\"font-size: 9.5pt;font-family: Consolas\"> orderdate <span style=\"color: gray\">&gt;= @date <\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">order<span style=\"font-size: 9.5pt;font-family: Consolas\"> <span style=\"color: blue\">by SalesOrderID<\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">option<span style=\"font-size: 9.5pt;font-family: Consolas;color: gray\">(<span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">recompile<span style=\"font-size: 9.5pt;font-family: Consolas;color: gray\">)<\/span><\/span><\/span><\/span><\/div>\n<p>&nbsp;<\/p>\n<p>Let\u2019s compare the estimated execution plan of the both queries. In fact, the estimated plan produced by the query optimizer is the same in both cases.<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_1_-_estimated_execution_plan.jpg\" alt=\"blog_35_-_1_-_estimated_execution_plan\" width=\"267\" height=\"99\" \/><\/p>\n<p>Next, let\u2019s compare their real execution plans.<\/p>\n<p>&#8211;&gt; Concerning the first query:<\/p>\n<p><span style=\"font-size: 11pt;line-height: 107%;font-family: 'Calibri',sans-serif;color: #000000\"><span style=\"font-size: 11pt;line-height: 107%;font-family: 'Calibri',sans-serif\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_2_-_real_execution_plan_query_1.jpg\" alt=\"blog_35_-_2_-_real_execution_plan_query_1\" width=\"299\" height=\"119\" \/><\/span><\/span><\/p>\n<p>&#8211;&gt; Concerning the second query (with recompile hint):<\/p>\n<p><span style=\"font-size: 11pt;line-height: 107%;font-family: 'Calibri',sans-serif\"><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_3_-_real_execution_plan_query_2.jpg\" alt=\"blog_35_-_3_-_real_execution_plan_query_2\" width=\"306\" height=\"132\" \/><\/span><br \/>\nInteresting, isn\u2019t it? For the same query except the recompile option the query optimizer has decided to use an index scan operator in the first case and an index seek in the second case. At this point, of course we supposed that the recompile hint affects the query optimizer decision but how far? Well, the response lies in the way the query optimizer handles the parameter @date.<\/p>\n<p>In the first query, the query optimizer is not aware of the @date parameter value at the compile time.<\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_4_-_real_execution_plan_query_1_-_predicate.jpg\" alt=\"blog_35_-_4_-_real_execution_plan_query_1_-_predicate\" width=\"505\" height=\"387\" \/><\/p>\n<p>It means that it will not be able to use the density information and instead it will use the standard guess of 30% selectivity for inequality comparisons. If we take a look at the statistic object of the primary key we may see that 30% of the total rows is equal to 31465 * 0.3 = 9439<\/p>\n<p>&nbsp;<\/p>\n<p style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">dbcc<span style=\"font-size: 9.5pt;font-family: Consolas\"> <span style=\"color: blue\">show_statistics <span style=\"color: gray\">(<span style=\"color: red\">&#8216;sales.salesorderheader&#8217;<span style=\"color: gray\">, <span style=\"color: red\">&#8216;PK_SalesOrderHeader_SalesOrderID&#8217;<span style=\"color: gray\">) <span style=\"color: blue\">with <span style=\"color: blue\">stat_header<span style=\"color: gray\">;<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/p>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_6_-_statistic_primary_key.jpg\" alt=\"blog_35_-_6_-_statistic_primary_key\" width=\"627\" height=\"30\" \/><\/p>\n<p>But at this point, we may wonder why SQL Server is using the primary key rather than the index on the orderdate column for example. In fact, the primary key is a good candidate for this query includes an ORDER BY clause (order by SalesOrderId). It means that data is already ordered by SalesOrderId and the query optimizer doesn\u2019t need to perform an extra step that consists in sorting data before using the top operator as shown below:<\/p>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">declare<span style=\"font-size: 9.5pt;font-family: Consolas\"> @date <span style=\"color: blue\">datetime <span style=\"color: gray\">= <span style=\"color: fuchsia\">getdate<span style=\"color: gray\">()<\/span><\/span><\/span><\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas\">\u00a0<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">select<span style=\"font-size: 9.5pt;font-family: Consolas\"> <span style=\"color: blue\">top 1 SalesOrderID <\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">from<span style=\"font-size: 9.5pt;font-family: Consolas\"> sales<span style=\"color: gray\">.salesorderheader<\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">where<span style=\"font-size: 9.5pt;font-family: Consolas\"> orderdate <span style=\"color: gray\">&gt;= @date <\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">order<span style=\"font-size: 9.5pt;font-family: Consolas\"> <span style=\"color: blue\">by SalesOrderID<\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas\">\u00a0<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">select<span style=\"font-size: 9.5pt;font-family: Consolas\"> <span style=\"color: blue\">top 1 SalesOrderID <\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">from<span style=\"font-size: 9.5pt;font-family: Consolas\"> sales<span style=\"color: gray\">.salesorderheader <span style=\"color: blue\">with <span style=\"color: gray\">(<span style=\"color: blue\">index <span style=\"color: gray\">(idx_sales_salesorderheader_orderdate<span style=\"color: gray\">) <span style=\"color: gray\">)<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">where<span style=\"font-size: 9.5pt;font-family: Consolas\"> orderdate <span style=\"color: gray\">&gt;= @date <\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">order<span style=\"font-size: 9.5pt;font-family: Consolas\"> <span style=\"color: blue\">by SalesOrderID<\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">go<\/span><\/div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_7_-_query_1_-_sort.jpg\" alt=\"blog_35_-_7_-_query_1_-_sort\" width=\"420\" height=\"240\" \/><\/p>\n<p>Let\u2019s continue with the second query and notice how SQL Server is handling the @date parameter this time: the local variable value is transformed as parameter as shown below:<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_8_-_real_execution_plan_query_2_-_predicate.jpg\" alt=\"blog_35_-_8_-_real_execution_plan_query_2_-_predicate\" width=\"489\" height=\"335\" \/><\/p>\n<p>Hmm\u2026 does it mean that SQL Server is aware of local variable value when using the statement RECOMPILE hint? In fact, yes it does and this detail changes completely the story because in this case SQL Server is able to use the histogram of the index idx_salesorderheader_orderdate. However, we are in such situation where the predicate value is not represented on the histogram and SQL Server will use an estimate of 1 that implies to use an index seek operator. Likewise, we may wonder why SQL Server has decided to seek the index idx_salesorderheader_orderdate with a TopN Sort operator here. Once again, the query optimizer is cost-based and this strategy is surely the less costly. Let\u2019s demonstrate by comparing the both strategies:<\/p>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">declare<span style=\"font-size: 9.5pt;font-family: Consolas\"> @date <span style=\"color: blue\">datetime <span style=\"color: gray\">= <span style=\"color: fuchsia\">getdate<span style=\"color: gray\">()<\/span><\/span><\/span><\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas\">\u00a0<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">select<span style=\"font-size: 9.5pt;font-family: Consolas\"> <span style=\"color: blue\">top 1 SalesOrderID <\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">from<span style=\"font-size: 9.5pt;font-family: Consolas\"> sales<span style=\"color: gray\">.salesorderheader <span style=\"color: blue\">with <span style=\"color: gray\">(<span style=\"color: blue\">index <span style=\"color: gray\">(PK_SalesOrderHeader_SalesOrderID<span style=\"color: gray\">) <span style=\"color: gray\">)<\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">where<span style=\"font-size: 9.5pt;font-family: Consolas\"> orderdate <span style=\"color: gray\">&gt;= @date <\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">order<span style=\"font-size: 9.5pt;font-family: Consolas\"> <span style=\"color: blue\">by SalesOrderID<\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">option<span style=\"font-size: 9.5pt;font-family: Consolas;color: gray\">(<span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">recompile<span style=\"font-size: 9.5pt;font-family: Consolas;color: gray\">)<\/span><\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas\">\u00a0<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">select<span style=\"font-size: 9.5pt;font-family: Consolas\"> <span style=\"color: blue\">top 1 SalesOrderID <\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">from<span style=\"font-size: 9.5pt;font-family: Consolas\"> sales<span style=\"color: gray\">.salesorderheader <\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">where<span style=\"font-size: 9.5pt;font-family: Consolas\"> orderdate <span style=\"color: gray\">&gt;= @date <\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">order<span style=\"font-size: 9.5pt;font-family: Consolas\"> <span style=\"color: blue\">by SalesOrderID<\/span><\/span><\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">option<span style=\"font-size: 9.5pt;font-family: Consolas;color: gray\">(<span style=\"font-size: 9.5pt;font-family: Consolas;color: blue\">recompile<span style=\"font-size: 9.5pt;font-family: Consolas;color: gray\">)<\/span><\/span><\/span><\/span><\/div>\n<p>&nbsp;<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" src=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_9_-_real_execution_plan_query__-_force_using_pk.jpg\" alt=\"blog_35_-_9_-_real_execution_plan_query__-_force_using_pk\" width=\"430\" height=\"247\" \/><br \/>\nLet\u2019s take a look at the statistics related on both CPU and IO consumption of the both queries<\/p>\n<p>&#8211;&gt; Concerning the first query (clustered index scan operator)<\/p>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9pt;font-family: Consolas\">Table &#8216;SalesOrderHeader&#8217;. Scan count 1, logical reads 689, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9pt;font-family: Consolas\">\u00a0<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9pt;font-family: Consolas\">(1 row(s) affected)<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9pt;font-family: Consolas\">\u00a0<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9pt;font-family: Consolas\"> SQL Server Execution Times:<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9pt;font-family: Consolas\">\u00a0\u00a0 CPU time = 16 ms, elapsed time = 2 ms.<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9pt;font-family: Consolas\">SQL Server parse and compile time: <\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9pt;font-family: Consolas\">\u00a0\u00a0 CPU time = 0 ms, elapsed time = 1 ms.<\/span><\/div>\n<p>&nbsp;<\/p>\n<p>&#8211;&gt; Concerning the second query (index seek operator). Yes, in this case you have only to read 2 pages (the index root page and one data page at leaf level)<\/p>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9pt;font-family: Consolas\">Table &#8216;SalesOrderHeader&#8217;. Scan count 1, logical reads 2, physical reads 0, read-ahead reads 0, lob logical reads 0, lob physical reads 0, lob read-ahead reads 0.<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9pt;font-family: Consolas\">\u00a0<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9pt;font-family: Consolas\">(1 row(s) affected)<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9pt;font-family: Consolas\">\u00a0<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9pt;font-family: Consolas\"> SQL Server Execution Times:<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9pt;font-family: Consolas\">\u00a0\u00a0 CPU time = 0 ms, elapsed time = 0 ms.<\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9pt;font-family: Consolas\">SQL Server parse and compile time: <\/span><\/div>\n<div style=\"margin-bottom: 0.0001pt;line-height: normal;background: none repeat scroll 0% 0% #e7e6e6\"><span style=\"font-size: 9pt;font-family: Consolas\">\u00a0\u00a0 CPU time = 0 ms, elapsed time = 0 ms.<\/span><\/div>\n<p>&nbsp;<\/p>\n<p>The bottom line: in certain cases we may be surprised by the choices made by the query optimizer but most of the time, we can trust it and this is what I wanted to point out in the blog post! The truth often lies elsewhere \ud83d\ude42<\/p>\n<p>Happy optimization!<\/p>\n<p><span style=\"color: #000000\">By David Barbarin<\/span><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Last night, I had an interesting discussion with one of my MVP French friend that faces a weird situation where a query that uses a statement level RECOMPILE hint produces an execution plan that disappointed him. He told me we can simulate the same situation by using the AdventureWorks database and Sales.SalesOrderHeader table. First, we [&hellip;]<\/p>\n","protected":false},"author":26,"featured_media":4454,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"_acf_changed":false,"footnotes":""},"categories":[197],"tags":[348,67,546,51],"type_dbi":[],"class_list":["post-4453","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-application-integration-middleware","tag-execution-plan","tag-performance","tag-recompile","tag-sql-server"],"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>A funny story with recompile hint at statement level - dbi Blog<\/title>\n<meta name=\"description\" content=\"A funny story with recompile hint at statement level\" \/>\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\/a-funny-story-with-recompile-hint-at-statement-level\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A funny story with recompile hint at statement level\" \/>\n<meta property=\"og:description\" content=\"A funny story with recompile hint at statement level\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/\" \/>\n<meta property=\"og:site_name\" content=\"dbi Blog\" \/>\n<meta property=\"article:published_time\" content=\"2015-03-18T14:31:48+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_1_-_estimated_execution_plan.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"334\" \/>\n\t<meta property=\"og:image:height\" content=\"124\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"author\" content=\"Microsoft Team\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"Microsoft Team\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"4 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\/a-funny-story-with-recompile-hint-at-statement-level\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/\"},\"author\":{\"name\":\"Microsoft Team\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bfab48333280d616e1170e7369df90a4\"},\"headline\":\"A funny story with recompile hint at statement level\",\"datePublished\":\"2015-03-18T14:31:48+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/\"},\"wordCount\":888,\"commentCount\":0,\"image\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_1_-_estimated_execution_plan.jpg\",\"keywords\":[\"Execution plan\",\"Performance\",\"recompile\",\"SQL Server\"],\"articleSection\":[\"Application integration &amp; Middleware\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/\",\"name\":\"A funny story with recompile hint at statement level - dbi Blog\",\"isPartOf\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_1_-_estimated_execution_plan.jpg\",\"datePublished\":\"2015-03-18T14:31:48+00:00\",\"author\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bfab48333280d616e1170e7369df90a4\"},\"description\":\"A funny story with recompile hint at statement level\",\"breadcrumb\":{\"@id\":\"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/#primaryimage\",\"url\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_1_-_estimated_execution_plan.jpg\",\"contentUrl\":\"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_1_-_estimated_execution_plan.jpg\",\"width\":334,\"height\":124},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Accueil\",\"item\":\"https:\/\/www.dbi-services.com\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"A funny story with recompile hint at statement level\"}]},{\"@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\/bfab48333280d616e1170e7369df90a4\",\"name\":\"Microsoft Team\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g\",\"caption\":\"Microsoft Team\"},\"url\":\"https:\/\/www.dbi-services.com\/blog\/author\/microsoft-team\/\"}]}<\/script>\n<!-- \/ Yoast SEO Premium plugin. -->","yoast_head_json":{"title":"A funny story with recompile hint at statement level - dbi Blog","description":"A funny story with recompile hint at statement level","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\/a-funny-story-with-recompile-hint-at-statement-level\/","og_locale":"en_US","og_type":"article","og_title":"A funny story with recompile hint at statement level","og_description":"A funny story with recompile hint at statement level","og_url":"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/","og_site_name":"dbi Blog","article_published_time":"2015-03-18T14:31:48+00:00","og_image":[{"width":334,"height":124,"url":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_1_-_estimated_execution_plan.jpg","type":"image\/jpeg"}],"author":"Microsoft Team","twitter_card":"summary_large_image","twitter_misc":{"Written by":"Microsoft Team","Est. reading time":"4 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/#article","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/"},"author":{"name":"Microsoft Team","@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bfab48333280d616e1170e7369df90a4"},"headline":"A funny story with recompile hint at statement level","datePublished":"2015-03-18T14:31:48+00:00","mainEntityOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/"},"wordCount":888,"commentCount":0,"image":{"@id":"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/#primaryimage"},"thumbnailUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_1_-_estimated_execution_plan.jpg","keywords":["Execution plan","Performance","recompile","SQL Server"],"articleSection":["Application integration &amp; Middleware"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/","url":"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/","name":"A funny story with recompile hint at statement level - dbi Blog","isPartOf":{"@id":"https:\/\/www.dbi-services.com\/blog\/#website"},"primaryImageOfPage":{"@id":"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/#primaryimage"},"image":{"@id":"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/#primaryimage"},"thumbnailUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_1_-_estimated_execution_plan.jpg","datePublished":"2015-03-18T14:31:48+00:00","author":{"@id":"https:\/\/www.dbi-services.com\/blog\/#\/schema\/person\/bfab48333280d616e1170e7369df90a4"},"description":"A funny story with recompile hint at statement level","breadcrumb":{"@id":"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/#primaryimage","url":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_1_-_estimated_execution_plan.jpg","contentUrl":"https:\/\/www.dbi-services.com\/blog\/wp-content\/uploads\/sites\/2\/2022\/04\/blog_35_-_1_-_estimated_execution_plan.jpg","width":334,"height":124},{"@type":"BreadcrumbList","@id":"https:\/\/www.dbi-services.com\/blog\/a-funny-story-with-recompile-hint-at-statement-level\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Accueil","item":"https:\/\/www.dbi-services.com\/blog\/"},{"@type":"ListItem","position":2,"name":"A funny story with recompile hint at statement level"}]},{"@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\/bfab48333280d616e1170e7369df90a4","name":"Microsoft Team","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c44a1a792c059f24055763aa77d80a244467f6eef724a8bd13db8d4a350b7a4c?s=96&d=mm&r=g","caption":"Microsoft Team"},"url":"https:\/\/www.dbi-services.com\/blog\/author\/microsoft-team\/"}]}},"_links":{"self":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/4453","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\/26"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/comments?post=4453"}],"version-history":[{"count":0,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/posts\/4453\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media\/4454"}],"wp:attachment":[{"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/media?parent=4453"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/categories?post=4453"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/tags?post=4453"},{"taxonomy":"type","embeddable":true,"href":"https:\/\/www.dbi-services.com\/blog\/wp-json\/wp\/v2\/type_dbi?post=4453"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}