{"id":12134,"date":"2019-05-10T23:24:04","date_gmt":"2019-05-10T23:24:04","guid":{"rendered":"https:\/\/dbtut.com\/?p=12134"},"modified":"2019-05-10T23:24:06","modified_gmt":"2019-05-10T23:24:06","slug":"query-hints-in-sql-server","status":"publish","type":"post","link":"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/","title":{"rendered":"Query Hints in SQL Server"},"content":{"rendered":"<h2>What is Query Hint?<\/h2>\n<p>You can change the execution plan of the query by adding the query hints to the end of the query. For example, if you add the maxdop query hint to the end of a query that uses a single CPU, the query may use multiple CPUs(parallelism). Or, you can use the recompile query hint to ensure that the query generates a query plan each time it is executed.<\/p>\n<p>Before you apply Query Hint, you need to make sure that the operation you want is correct. Because using query hint, you are changing SQL Server&#8217;s normal behavior. The scripts mentioned in the article were taken from microsoft&#8217;s site and tested on the AdventureWorks2012 database.<\/p>\n<p>Below, I share some examples of query hints that I think you might find useful.<\/p>\n<h2>Recompile Query Hint<\/h2>\n<p>When a stored procedure or parameterized query executed, SQL Server creates a query plan as soon as they are running. The second time they are executed, they are not compiled again, they use the query plan they produced earlier. In this way, they do not need to be recompiled each time.<\/p>\n<p>This is SQL Server&#8217;s default behaviour. You can change this in many ways. For Example; &#8220;<a href=\"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/optimize-for-ad-hoc-workloads-and-parameterization-in-sql-server\/\" target=\"_blank\" rel=\"noopener noreferrer\">Optimize for ad hoc workloads and Parameterization in SQL Server<\/a>&#8221; or Query Hints.<\/p>\n<p>The normal behavior of SQL Server often improves performance. However, if the data distribution in the table is uneven, sometimes it causes parameter sniffing. In the article &#8220;<a href=\"https:\/\/dbtut.com\/index.php\/2018\/07\/20\/what-is-paramater-sniffing\/\" target=\"_blank\" rel=\"noopener noreferrer\">What is Parameter Sniffing<\/a>&#8221; you will find details about the parameter sniffing. To resolve this issue, you can use Recompile Query Hint to generate a query plan each time the query is executed. In the article &#8220;<a href=\"https:\/\/dbtut.com\/index.php\/2018\/07\/20\/what-is-paramater-sniffing\/\" target=\"_blank\" rel=\"noopener noreferrer\">What is Parameter Sniffing<\/a>&#8221; you can find the example of using Recompile Query Hint.<\/p>\n<h2>OPTIMIZE FOR Query Hint<\/h2>\n<p>Optimize For Query Hint is the query hint that instructs the query optimizer to use a specific value for a local variable when creating the Query Plan. Like RECOMPILE query hint, this can be useful for parameter sniffing. In my article &#8220;<a href=\"https:\/\/dbtut.com\/index.php\/2018\/07\/20\/what-is-paramater-sniffing\/\" target=\"_blank\" rel=\"noopener noreferrer\">What is Parameter Sniffing<\/a>&#8221; you can find an example with this type of query hint.<\/p>\n<h2>USE PLAN Query Hint<\/h2>\n<p>Use Plan Query Hint is the query hint that instructs the query optimizer to use an existing query plan for a query when creating the Query Plan.<\/p>\n<p>If there is long-running queries because of they use the wrong query plan, and you know that there is a better query plan, you can use this query hint.<\/p>\n<p>But my advice is to leave this job to sql server and keep your statistics up to date. You may want to read the article &#8220;<a href=\"https:\/\/dbtut.com\/index.php\/2018\/06\/13\/statistic-concept-and-performance-effect-on-sql-server\/\" target=\"_blank\" rel=\"noopener noreferrer\">Statistic Concept and Performance Effect on SQL Server<\/a>&#8221; for detailed information on statistics.<\/p>\n<p>However, if you want to use the use plan query hint, you can find details about how to use it in the link below.<\/p>\n<p><a href=\"https:\/\/technet.microsoft.com\/en-us\/library\/ms186954(v=sql.105).aspx\" target=\"_blank\" rel=\"noopener noreferrer\">https:\/\/technet.microsoft.com\/en-us\/library\/ms186954(v=sql.105).aspx<\/a><\/p>\n<h2>PARAMETERIZATION Query Hint<\/h2>\n<p>The behavior of some queries changes when we set parameterization in the database. With the Template plan guide, we can change the parameterization method for specific queries, difference from the parameterization method set at the database level.<\/p>\n<p>Read the following article for Template Plan Guide.<\/p>\n<p>&#8220;<a href=\"https:\/\/dbtut.com\/wp-admin\/post.php?post=12105&amp;action=edit\">Increase Query Performance in SQL Server Using sp_create_plan_guide System Stored Procedure<\/a>&#8221;<\/p>\n<p>Read the following article for Parameterization.<\/p>\n<p>&#8220;<a href=\"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/optimize-for-ad-hoc-workloads-and-parameterization-in-sql-server\/\" target=\"_blank\" rel=\"noopener noreferrer\">Optimize for ad hoc workloads and Parameterization<\/a>&#8221;<\/p>\n<h2>MERGE JOIN Query Hint<\/h2>\n<p>The following example uses a query hint that forces join to MERGE JOIN. For more information about join operations in SQL Server, please refer to &#8220;<a href=\"https:\/\/dbtut.com\/index.php\/2018\/06\/20\/join-types-in-sql-server\/\" target=\"_blank\" rel=\"noopener noreferrer\">Join Types in SQL Server<\/a>&#8221; and &#8220;<a href=\"https:\/\/dbtut.com\/index.php\/2018\/06\/23\/join-types-in-sql-server-execution-plan\/\" target=\"_blank\" rel=\"noopener noreferrer\">JOIN Types in SQL Server Execution Plan<\/a>&#8220;.<\/p>\n<pre class=\"lang:default decode:true \">SELECT *&nbsp;&nbsp; \nFROM Sales.Customer AS c&nbsp; \nINNER JOIN Sales.CustomerAddress AS ca ON c.CustomerID = ca.CustomerID&nbsp; \nWHERE TerritoryID = 5&nbsp; \nOPTION (MERGE JOIN);&nbsp; \nGO<\/pre>\n<h2>MAXRECURSION Query Hint<\/h2>\n<p>MAXRECURSION Query Hint prevents a poorly designed CTE (Common Table Expression) from entering the infinite loop. For more information about CTE (Common Table Expression), you may want to read the article &#8220;<a href=\"https:\/\/dbtut.com\/index.php\/2019\/02\/16\/common-table-expressioncte\/\" target=\"_blank\" rel=\"noopener noreferrer\">Common Table Expression (CTE)<\/a>&#8220;.<\/p>\n<p>The following example creates a CTE that enters an infinite loop, and sets the maximum number of loops of the query that uses the CTE.<\/p>\n<pre class=\"lang:default decode:true\">--CTE that enters an infinite loop\nWITH cte (CustomerID, PersonID, StoreID) AS&nbsp; \n(&nbsp; \n&nbsp;&nbsp;&nbsp;&nbsp;SELECT CustomerID, PersonID, StoreID&nbsp; \n&nbsp;&nbsp;&nbsp;&nbsp;FROM Sales.Customer&nbsp; \n&nbsp;&nbsp;&nbsp;&nbsp;WHERE PersonID IS NOT NULL&nbsp; \n&nbsp;&nbsp;UNION ALL&nbsp; \n&nbsp;&nbsp;&nbsp;&nbsp;SELECT cte.CustomerID, cte.PersonID, cte.StoreID&nbsp; \n&nbsp;&nbsp;&nbsp;&nbsp;FROM cte&nbsp;&nbsp; \n&nbsp;&nbsp;&nbsp;&nbsp;JOIN&nbsp; Sales.Customer AS e&nbsp;&nbsp; \n&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;ON cte.PersonID = e.CustomerID&nbsp; \n)&nbsp; \n--Query using CTE and using the MAXRECURSION query hint\nSELECT CustomerID, PersonID, StoreID&nbsp; \nFROM cte&nbsp; \nOPTION (MAXRECURSION 2);&nbsp; \n\nGO<\/pre>\n<p>When I execute the query in the database, it entered the loop 2 times and terminated with an error like the following.<\/p>\n<p><span style=\"color: #ff0000;\"><em>Msg 530, Level 16, State 1, Line 1<\/em><\/span><\/p>\n<p><span style=\"color: #ff0000;\"><em>The statement terminated. The maximum recursion 2 has been exhausted before statement completion.<\/em><\/span><\/p>\n<p id=\"HzYsYlj\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-12143  aligncenter\" src=\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/05\/img_5cd5f64b2840a.png\" alt=\"\" width=\"768\" height=\"432\"><\/p>\n<p>Removing the OPTION (MAXRECURSION 2) indian in the query changed the error as follows. I performed this test on SQL Server 2014. After the query entered the loop 100 times, SQL Server automatically stopped the script.<\/p>\n<p><span style=\"color: #ff0000;\"><em>Msg 530, Level 16, State 1, Line 1<\/em><\/span><\/p>\n<p><span style=\"color: #ff0000;\"><em>The statement terminated. The maximum recursion 100 has been exhausted before statement completion.<\/em><\/span><\/p>\n<p id=\"FyAOTqz\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-12145  aligncenter\" src=\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/05\/img_5cd5f6d5db23d.png\" alt=\"\" width=\"780\" height=\"424\"><\/p>\n<h2>MAXDOP Query Hint<\/h2>\n<p>By specifing OPTION (MAXDOP 2) hint at the end of the query, we have specified that the maxdop setting of this query will be 2 regardless of the instance setting. Thus, this query will work two parallel.<\/p>\n<p>It is a query hint that will be useful for report queries. But be careful while using. Because, if many report queries works in parallel, you may encounter CPU bottleneck. You may want to read the below article about MAXDOP and CPU usage.<\/p>\n<p>&#8220;<a href=\"https:\/\/dbtut.com\/index.php\/2018\/10\/25\/numa-nodes-max-min-server-memory-log-pages-in-memory-and-maxdop\/\" target=\"_blank\" rel=\"bookmark noopener noreferrer\">Numa Nodes, MAX\/MIN Server Memory, Log Pages In Memory and MAXDOP<\/a>&#8221;<\/p>\n<p>You can use MAXDOP Query hint as follows.<\/p>\n<pre class=\"lang:default decode:true\">SELECT ProductID, OrderQty, SUM(LineTotal) AS Total \nFROM Sales.SalesOrderDetail \nWHERE UnitPrice &lt; $5.00 GROUP BY ProductID, OrderQty \nORDER BY ProductID, OrderQty OPTION (MAXDOP 2);<\/pre>\n<h2>FORCE INDEX Query Hint<\/h2>\n<p>In SQL Server, we use this hint to tell a query to work with the specified index. Actually SQL Server almost always makes the best choice. So if you keep your statistics up-to-date and rebuild your indexes on a regular basis, using this hint will usually slow you down.<\/p>\n<p>I suggest you read the following articles about statistics and indexes.<\/p>\n<p>&#8220;<a href=\"https:\/\/dbtut.com\/index.php\/2018\/06\/12\/index-concept-and-performance-effect-on-sql-server\/\" target=\"_blank\" rel=\"bookmark noopener noreferrer\">Index Concept and Performance Effect on SQL Server&#8221;<\/a>,<\/p>\n<p>&#8220;<a href=\"https:\/\/dbtut.com\/index.php\/2018\/06\/13\/statistic-concept-and-performance-effect-on-sql-server\/\" target=\"_blank\" rel=\"bookmark noopener noreferrer\">Statistic Concept and Performance Effect on SQL Server&#8221;<\/a><\/p>\n<p>Let&#8217;s take a look at the execution plan of the following select statement by clicking the checkbox in the following screen. As you can see, under normal circumstances Clustred Index Scan process is performed as follows.<\/p>\n<pre class=\"lang:default decode:true \">USE AdventureWorks2014Yeni\nGO\nSELECT *\nFROM Person.Person\nGO\n<\/pre>\n<p id=\"LKPeHmG\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-12148  aligncenter\" src=\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/05\/img_5cd5fefb403e5.png\" alt=\"\" width=\"724\" height=\"724\"><\/p>\n<p>Let&#8217;s add a hint to the query. As you can see, it used the Index we specified and then it had to perform Key Lookup to bring the needed records.<\/p>\n<p id=\"EpdILVD\"><img loading=\"lazy\" decoding=\"async\" class=\" wp-image-12149  aligncenter\" src=\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/05\/img_5cd5ff574f4cb.png\" alt=\"\" width=\"745\" height=\"635\"><\/p>\n<p>I wanted to show it because it could be needed in very rare cases, but don&#8217;t use it unless it&#8217;s really necessary. Because as I mentioned before, it will mostly decrease your performance.<\/p>\n<h2>Nolock Query Hint<\/h2>\n<p>This query hint force query to work like read uncommitted. Usually dbas advice to software developers due to locking. But be careful using this query hint. Because if you use this hint in select statements, it reads dirty data.<\/p>\n<p>I suggest you read the following articles.<\/p>\n<p>&#8220;<a href=\"https:\/\/dbtut.com\/index.php\/2018\/06\/27\/isolation-levels-1\/\" target=\"_blank\" rel=\"noopener noreferrer\">Isolation Levels 1<\/a>&#8220;,<\/p>\n<p>&#8220;<a href=\"https:\/\/dbtut.com\/index.php\/2018\/06\/28\/isolation-levels-2\/\" target=\"_blank\" rel=\"noopener noreferrer\">Isolation Levels 2<\/a>&#8220;,<\/p>\n<p>&#8220;<a href=\"https:\/\/dbtut.com\/index.php\/2018\/06\/28\/isolation-levels-3\/\" target=\"_blank\" rel=\"noopener noreferrer\">Isolation Levels 3<\/a>&#8220;,<\/p>\n<p><strong>Example:<\/strong><\/p>\n<pre class=\"lang:default decode:true \">SELECT *  FROM [AdventureWorks2012].[Person].[Address] WITH(NOLOCK)<\/pre>\n<h2>Other Query Hints<\/h2>\n<table border=\"1\">\n<tbody>\n<tr>\n<td>ROWLOCK<\/td>\n<td>It force the query to place the row-level lock instead of the page or table-level locking<\/td>\n<\/tr>\n<tr>\n<td>TABLOCK<\/td>\n<td>It force the query to place the table-level lock instead of the page or row-level locking<\/td>\n<\/tr>\n<tr>\n<td>TABLOCKX<\/td>\n<td>It force the query to place the&nbsp; exclusive lock on the entire table.<\/td>\n<\/tr>\n<tr>\n<td>UPDLOCK<\/td>\n<td>It force the query to place the&nbsp; update lock. This guarantees that the data will be same until the update will be finished.<\/td>\n<\/tr>\n<tr>\n<td>XLOCK<\/td>\n<td>It force the query to place the&nbsp; exclusive lock<\/td>\n<\/tr>\n<\/tbody>\n<\/table>\n<h4>Sample Usage<\/h4>\n<pre class=\"lang:default decode:true\">SELECT *  FROM [AdventureWorks2012].[Person].[Address] WITH(XLOCK)<\/pre>\n<p>You may want to read the below article about lock types in SQL Server.<\/p>\n<p>&#8220;<a href=\"https:\/\/dbtut.com\/index.php\/2018\/08\/24\/sql-server-lock-types\/\" target=\"_blank\" rel=\"noopener noreferrer\">SQL Server Lock Types<\/a>&#8221;<\/p>\n\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_12134\" class=\"pvc_stats all  \" data-element-id=\"12134\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/dbtut.com\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"excerpt":{"rendered":"<p>What is Query Hint? You can change the execution plan of the query by adding the query hints to the end of the query. For example, if you add the maxdop query hint to the end of a query that uses a single CPU, the query may use multiple CPUs(parallelism). Or, you can use the &hellip;<\/p>\n<div class=\"pvc_clear\"><\/div>\n<p id=\"pvc_stats_12134\" class=\"pvc_stats all  \" data-element-id=\"12134\" style=\"\"><i class=\"pvc-stats-icon medium\" aria-hidden=\"true\"><svg aria-hidden=\"true\" focusable=\"false\" data-prefix=\"far\" data-icon=\"chart-bar\" role=\"img\" xmlns=\"http:\/\/www.w3.org\/2000\/svg\" viewBox=\"0 0 512 512\" class=\"svg-inline--fa fa-chart-bar fa-w-16 fa-2x\"><path fill=\"currentColor\" d=\"M396.8 352h22.4c6.4 0 12.8-6.4 12.8-12.8V108.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v230.4c0 6.4 6.4 12.8 12.8 12.8zm-192 0h22.4c6.4 0 12.8-6.4 12.8-12.8V140.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v198.4c0 6.4 6.4 12.8 12.8 12.8zm96 0h22.4c6.4 0 12.8-6.4 12.8-12.8V204.8c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v134.4c0 6.4 6.4 12.8 12.8 12.8zM496 400H48V80c0-8.84-7.16-16-16-16H16C7.16 64 0 71.16 0 80v336c0 17.67 14.33 32 32 32h464c8.84 0 16-7.16 16-16v-16c0-8.84-7.16-16-16-16zm-387.2-48h22.4c6.4 0 12.8-6.4 12.8-12.8v-70.4c0-6.4-6.4-12.8-12.8-12.8h-22.4c-6.4 0-12.8 6.4-12.8 12.8v70.4c0 6.4 6.4 12.8 12.8 12.8z\" class=\"\"><\/path><\/svg><\/i> <img loading=\"lazy\" decoding=\"async\" width=\"16\" height=\"16\" alt=\"Loading\" src=\"https:\/\/dbtut.com\/wp-content\/plugins\/page-views-count\/ajax-loader-2x.gif\" border=0 \/><\/p>\n<div class=\"pvc_clear\"><\/div>\n","protected":false},"author":1,"featured_media":12152,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"om_disable_all_campaigns":false,"_monsterinsights_skip_tracking":false,"_monsterinsights_sitenote_active":false,"_monsterinsights_sitenote_note":"","_monsterinsights_sitenote_category":0,"_uf_show_specific_survey":0,"_uf_disable_surveys":false,"footnotes":""},"categories":[3],"tags":[4073,4100,4076,4099,4075,4101,4102,4103,4108,4107,4111,4106,4105,4109,4112,4080,4104,4098,4081,4082,4067,4066,4083,4110,4068,4095,4094,4072,4093,4092,4070,4087,4069,4113,4089,4079,4074,4085,4086,4078,4077,4088,4090,4097,4096,4091,4071,4084],"class_list":["post-12134","post","type-post","status-publish","format-standard","has-post-thumbnail","","category-mssql","tag-does-nolock-improve-performance","tag-force-query-to-use-index","tag-how-do-you-use-nolock","tag-how-to-use-index-in-select-query-in-sql-server","tag-is-read-uncommitted-the-same-as-nolock","tag-maxdop-query-hint","tag-maxdop-query-hint-in-sql-server","tag-maxrecursion-query-hint","tag-merge-join-query-hint","tag-optimize-for-parameter-value","tag-optimize-for-query-hint","tag-option-clause-in-sql-server","tag-parallel-hint-in-sql-server","tag-parameterization-query-hint","tag-recompile-query-hint","tag-rowlock","tag-sql-server-index-hint","tag-sql-server-query-hints","tag-tablock","tag-tablockx","tag-the-statement-terminated-the-maximum-recursion-100-has-been-exhausted-before-statement-completion","tag-the-statement-terminated-the-maximum-recursion-2-has-been-exhausted-before-statement-completion","tag-updlock","tag-use-plan-query-hint","tag-what-are-hints-in-sql","tag-what-is-a-table-hint","tag-what-is-force-index-sql","tag-what-is-hint-in-sql-server-with-example","tag-what-is-index-hint-in-sql-server","tag-what-is-nolock-and-rowlock-in-sql-server","tag-what-is-nolock-hint-in-sql-server","tag-what-is-nolock-in-sql-server","tag-what-is-option-recompile-in-sql-server","tag-what-is-query-hint","tag-what-is-rowlock-in-sql-server","tag-what-is-sql-parallelism","tag-what-is-sql-server-maxdop","tag-what-is-tablock-in-sql-server","tag-what-is-tablockx-in-sql-server","tag-what-is-the-use-of-nolock-in-sql-server","tag-what-is-the-use-of-nolock-in-sql-server-2008","tag-what-is-updlck-in-sql-server","tag-what-is-xlock-in-sql-server","tag-what-is-xlock","tag-when-should-we-use-nolock-in-sql","tag-which-hints-is-used-to-control-the-locking-mechanism-of-tables","tag-why-nolock-is-used-sql-server","tag-xlock"],"aioseo_notices":[],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v24.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>Query Hints in SQL Server - Database Tutorials<\/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:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Query Hints in SQL Server - Database Tutorials\" \/>\n<meta property=\"og:description\" content=\"What is Query Hint? You can change the execution plan of the query by adding the query hints to the end of the query. For example, if you add the maxdop query hint to the end of a query that uses a single CPU, the query may use multiple CPUs(parallelism). Or, you can use the &hellip;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/\" \/>\n<meta property=\"og:site_name\" content=\"Database Tutorials\" \/>\n<meta property=\"article:published_time\" content=\"2019-05-10T23:24:04+00:00\" \/>\n<meta property=\"article:modified_time\" content=\"2019-05-10T23:24:06+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/05\/Ads\u0131z-22.png\" \/>\n\t<meta property=\"og:image:width\" content=\"776\" \/>\n\t<meta property=\"og:image:height\" content=\"431\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/png\" \/>\n<meta name=\"author\" content=\"dbtut\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"dbtut\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"7 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/\"},\"author\":{\"name\":\"dbtut\",\"@id\":\"https:\/\/dbtut.com\/#\/schema\/person\/fc047c39e1e53dce28fc4253529ea408\"},\"headline\":\"Query Hints in SQL Server\",\"datePublished\":\"2019-05-10T23:24:04+00:00\",\"dateModified\":\"2019-05-10T23:24:06+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/\"},\"wordCount\":1202,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/dbtut.com\/#organization\"},\"image\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/05\/Ads\u0131z-22.png\",\"keywords\":[\"Does Nolock improve performance?\",\"force query to use index\",\"How do you use Nolock?\",\"how to use index in select query in sql server\",\"Is read uncommitted the same as Nolock?\",\"MAXDOP Query Hint\",\"MAXDOP Query Hint in sql server\",\"MAXRECURSION Query Hint\",\"MERGE JOIN Query Hint\",\"optimize for (@parameter = value)\",\"OPTIMIZE FOR Query Hint\",\"option clause in sql server\",\"parallel hint in sql server\",\"PARAMETERIZATION Query Hint\",\"Recompile Query Hint\",\"ROWLOCK\",\"sql server index hint\",\"sql server query hints\",\"TABLOCK\",\"TABLOCKX\",\"The statement terminated. The maximum recursion 100 has been exhausted before statement completion.\",\"The statement terminated. The maximum recursion 2 has been exhausted before statement completion.\",\"UPDLOCK\",\"USE PLAN Query Hint\",\"What are hints in SQL?\",\"What is a table hint?\",\"What is force index SQL?\",\"What is hint in SQL Server with example?\",\"What is index hint in SQL Server?\",\"What is Nolock and Rowlock in SQL Server?\",\"What is Nolock hint in SQL Server?\",\"What is NOLOCK in SQL Server?\",\"What is option recompile in SQL Server?\",\"What is Query Hint?\",\"What is ROWLOCK in SQL Server?\",\"What is SQL parallelism?\",\"What is SQL Server Maxdop?\",\"What is Tablock in SQL Server?\",\"What is Tablockx in SQL Server?\",\"What is the use of Nolock in SQL Server\",\"What is the use of Nolock in SQL Server 2008?\",\"What is UPDLCK in SQL Server?\",\"What is XLOCK in SQL Server?\",\"What is Xlock?\",\"When should we use Nolock in SQL?\",\"Which hints is used to control the locking mechanism of tables?\",\"Why Nolock is used SQL Server?\",\"XLOCK\"],\"articleSection\":[\"MSSQL\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/\",\"url\":\"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/\",\"name\":\"Query Hints in SQL Server - Database Tutorials\",\"isPartOf\":{\"@id\":\"https:\/\/dbtut.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/#primaryimage\"},\"image\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/#primaryimage\"},\"thumbnailUrl\":\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/05\/Ads\u0131z-22.png\",\"datePublished\":\"2019-05-10T23:24:04+00:00\",\"dateModified\":\"2019-05-10T23:24:06+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/#primaryimage\",\"url\":\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/05\/Ads\u0131z-22.png\",\"contentUrl\":\"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/05\/Ads\u0131z-22.png\",\"width\":776,\"height\":431},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/dbtut.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Query Hints in SQL Server\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/dbtut.com\/#website\",\"url\":\"https:\/\/dbtut.com\/\",\"name\":\"Database Tutorials\",\"description\":\"MSSQL, Oracle, PostgreSQL, MySQL, MariaDB, DB2, Sybase, Teradata, Big Data, NOSQL, MongoDB, Couchbase, Cassandra, Windows, Linux\",\"publisher\":{\"@id\":\"https:\/\/dbtut.com\/#organization\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/dbtut.com\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-US\"},{\"@type\":\"Organization\",\"@id\":\"https:\/\/dbtut.com\/#organization\",\"name\":\"dbtut\",\"url\":\"https:\/\/dbtut.com\/\",\"logo\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/dbtut.com\/#\/schema\/logo\/image\/\",\"url\":\"https:\/\/dbtut.com\/wp-content\/uploads\/2021\/02\/dbtutlogo.jpg\",\"contentUrl\":\"https:\/\/dbtut.com\/wp-content\/uploads\/2021\/02\/dbtutlogo.jpg\",\"width\":223,\"height\":36,\"caption\":\"dbtut\"},\"image\":{\"@id\":\"https:\/\/dbtut.com\/#\/schema\/logo\/image\/\"}},{\"@type\":\"Person\",\"@id\":\"https:\/\/dbtut.com\/#\/schema\/person\/fc047c39e1e53dce28fc4253529ea408\",\"name\":\"dbtut\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/dbtut.com\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/c322c32021bf651d9e103b183963c479a9c9791ead0715f4348203496c39aa54?s=96&d=mm&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/c322c32021bf651d9e103b183963c479a9c9791ead0715f4348203496c39aa54?s=96&d=mm&r=g\",\"caption\":\"dbtut\"},\"description\":\"We are a team with over 10 years of database management and BI experience. Our Expertises: Oracle, SQL Server, PostgreSQL, MySQL, MongoDB, Elasticsearch, Kibana, Grafana.\",\"sameAs\":[\"http:\/\/NurullahCAKIR\"],\"url\":\"https:\/\/dbtut.com\/index.php\/author\/dbtut\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Query Hints in SQL Server - Database Tutorials","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:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/","og_locale":"en_US","og_type":"article","og_title":"Query Hints in SQL Server - Database Tutorials","og_description":"What is Query Hint? You can change the execution plan of the query by adding the query hints to the end of the query. For example, if you add the maxdop query hint to the end of a query that uses a single CPU, the query may use multiple CPUs(parallelism). Or, you can use the &hellip;","og_url":"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/","og_site_name":"Database Tutorials","article_published_time":"2019-05-10T23:24:04+00:00","article_modified_time":"2019-05-10T23:24:06+00:00","og_image":[{"width":776,"height":431,"url":"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/05\/Ads\u0131z-22.png","type":"image\/png"}],"author":"dbtut","twitter_card":"summary_large_image","twitter_misc":{"Written by":"dbtut","Est. reading time":"7 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/#article","isPartOf":{"@id":"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/"},"author":{"name":"dbtut","@id":"https:\/\/dbtut.com\/#\/schema\/person\/fc047c39e1e53dce28fc4253529ea408"},"headline":"Query Hints in SQL Server","datePublished":"2019-05-10T23:24:04+00:00","dateModified":"2019-05-10T23:24:06+00:00","mainEntityOfPage":{"@id":"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/"},"wordCount":1202,"commentCount":0,"publisher":{"@id":"https:\/\/dbtut.com\/#organization"},"image":{"@id":"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/#primaryimage"},"thumbnailUrl":"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/05\/Ads\u0131z-22.png","keywords":["Does Nolock improve performance?","force query to use index","How do you use Nolock?","how to use index in select query in sql server","Is read uncommitted the same as Nolock?","MAXDOP Query Hint","MAXDOP Query Hint in sql server","MAXRECURSION Query Hint","MERGE JOIN Query Hint","optimize for (@parameter = value)","OPTIMIZE FOR Query Hint","option clause in sql server","parallel hint in sql server","PARAMETERIZATION Query Hint","Recompile Query Hint","ROWLOCK","sql server index hint","sql server query hints","TABLOCK","TABLOCKX","The statement terminated. The maximum recursion 100 has been exhausted before statement completion.","The statement terminated. The maximum recursion 2 has been exhausted before statement completion.","UPDLOCK","USE PLAN Query Hint","What are hints in SQL?","What is a table hint?","What is force index SQL?","What is hint in SQL Server with example?","What is index hint in SQL Server?","What is Nolock and Rowlock in SQL Server?","What is Nolock hint in SQL Server?","What is NOLOCK in SQL Server?","What is option recompile in SQL Server?","What is Query Hint?","What is ROWLOCK in SQL Server?","What is SQL parallelism?","What is SQL Server Maxdop?","What is Tablock in SQL Server?","What is Tablockx in SQL Server?","What is the use of Nolock in SQL Server","What is the use of Nolock in SQL Server 2008?","What is UPDLCK in SQL Server?","What is XLOCK in SQL Server?","What is Xlock?","When should we use Nolock in SQL?","Which hints is used to control the locking mechanism of tables?","Why Nolock is used SQL Server?","XLOCK"],"articleSection":["MSSQL"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/","url":"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/","name":"Query Hints in SQL Server - Database Tutorials","isPartOf":{"@id":"https:\/\/dbtut.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/#primaryimage"},"image":{"@id":"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/#primaryimage"},"thumbnailUrl":"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/05\/Ads\u0131z-22.png","datePublished":"2019-05-10T23:24:04+00:00","dateModified":"2019-05-10T23:24:06+00:00","breadcrumb":{"@id":"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/#primaryimage","url":"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/05\/Ads\u0131z-22.png","contentUrl":"https:\/\/dbtut.com\/wp-content\/uploads\/2019\/05\/Ads\u0131z-22.png","width":776,"height":431},{"@type":"BreadcrumbList","@id":"https:\/\/dbtut.com\/index.php\/2019\/05\/10\/query-hints-in-sql-server\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/dbtut.com\/"},{"@type":"ListItem","position":2,"name":"Query Hints in SQL Server"}]},{"@type":"WebSite","@id":"https:\/\/dbtut.com\/#website","url":"https:\/\/dbtut.com\/","name":"Database Tutorials","description":"MSSQL, Oracle, PostgreSQL, MySQL, MariaDB, DB2, Sybase, Teradata, Big Data, NOSQL, MongoDB, Couchbase, Cassandra, Windows, Linux","publisher":{"@id":"https:\/\/dbtut.com\/#organization"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/dbtut.com\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-US"},{"@type":"Organization","@id":"https:\/\/dbtut.com\/#organization","name":"dbtut","url":"https:\/\/dbtut.com\/","logo":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/dbtut.com\/#\/schema\/logo\/image\/","url":"https:\/\/dbtut.com\/wp-content\/uploads\/2021\/02\/dbtutlogo.jpg","contentUrl":"https:\/\/dbtut.com\/wp-content\/uploads\/2021\/02\/dbtutlogo.jpg","width":223,"height":36,"caption":"dbtut"},"image":{"@id":"https:\/\/dbtut.com\/#\/schema\/logo\/image\/"}},{"@type":"Person","@id":"https:\/\/dbtut.com\/#\/schema\/person\/fc047c39e1e53dce28fc4253529ea408","name":"dbtut","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/dbtut.com\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/c322c32021bf651d9e103b183963c479a9c9791ead0715f4348203496c39aa54?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/c322c32021bf651d9e103b183963c479a9c9791ead0715f4348203496c39aa54?s=96&d=mm&r=g","caption":"dbtut"},"description":"We are a team with over 10 years of database management and BI experience. Our Expertises: Oracle, SQL Server, PostgreSQL, MySQL, MongoDB, Elasticsearch, Kibana, Grafana.","sameAs":["http:\/\/NurullahCAKIR"],"url":"https:\/\/dbtut.com\/index.php\/author\/dbtut\/"}]}},"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/posts\/12134","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/comments?post=12134"}],"version-history":[{"count":0,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/posts\/12134\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/media\/12152"}],"wp:attachment":[{"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/media?parent=12134"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/categories?post=12134"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/dbtut.com\/index.php\/wp-json\/wp\/v2\/tags?post=12134"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}