SQL Server Performance Tuning

Forcing Index, Order, And Join Hints And Being Careful

Updated
4 min read
Written by
Mark Varnas

Why should you care about SQL Server query hints?

Hints in SQL queries is like a double-edged sword.

While you might find a better query plan based on the current data, you are taking away SQL Server’s ability to adapt to changes.

Every time enough data in one of the tables has changed, SQL Server, by default, reviews the query accessing that table to see if it can come up with a better plan.

It may not do that with queries on which you have forced its way.

Hints can also be dangerous.

In case of a query is using an index hint, what happens when you run it If the index no longer exists? The code breaks and you get an error.

Believe in the SQL Server Query Optimizer; it typically selects the best execution plan for a query.

We recommend only using query hints as a last resort for experienced developers and DBAs.

How can I check mySQL Server?

The queries below use the Dynamic Management Views (DMVs) sys.dm_exec_query_optimizer_info. You can use them to know how many times an order or join hint have been used since the last SQL Server restart.

Number of times of “join” hinting

SELECT occurrence AS Count -- Number of times of "join" hinting recorded since the restart
FROM sys.dm_exec_query_optimizer_info
WHERE counter = 'join hint'
	AND occurrence > 500;

Number of times of “order” hinting


SELECT occurrence AS Count --Number of times of "order" hinting recorded since the restart
FROM sys.dm_exec_query_optimizer_info
WHERE counter = 'order hint'
	AND occurrence > 500;

Queries in the execution plan cache using an index hint

You can easily find queries in the execution plan cache using an index hint using the following query:

SELECT querystats.plan_handle
	,querystats.query_hash
	,SUBSTRING(sqltext.TEXT, (querystats.statement_start_offset / 2) + 1, (
			CASE querystats.statement_end_offset
				WHEN - 1
					THEN DATALENGTH(sqltext.TEXT)
				ELSE querystats.statement_end_offset
				END - querystats.statement_start_offset
			) / 2 + 1) AS sqltext
	,querystats.execution_count
	,querystats.total_logical_reads
	,querystats.total_logical_writes
	,querystats.creation_time
	,querystats.last_execution_time
	,CAST(query_plan AS XML) AS plan_xml
FROM sys.dm_exec_query_stats AS querystats
CROSS APPLY sys.dm_exec_text_query_plan(querystats.plan_handle, querystats.statement_start_offset, querystats.statement_end_offset) AS textplan
CROSS APPLY sys.dm_exec_sql_text(querystats.sql_handle) AS sqltext
WHERE textplan.query_plan LIKE N'%ForcedIndex="1"%'
	AND UPPER(sqltext.TEXT) LIKE N'%INDEX%'
OPTION (RECOMPILE)

How to fix the problem?

  1. Talk to the developers and try to figure out why and how they’re using order, join and index hints.
  2. You might get better performance if you clean the code.
  3. Remember to keep track and monitor the runtime of queries that you have changed.

More information

Article by
Mark Varnas
Founder | CEO | SQL Veteran
Hey, I'm Mark, one of the guys behind Red9. I make a living performance tuning SQL Servers and making them more stable.

Managed SQL Server services, consulting, and emergency support from expert DBAs to improve performance, predictability, and cost.

Get started with Red9 today.

Contact us

Discover More

SQL Server Health Check SQL Server Migrations & Upgrades SQL Server Performance Tuning SQL Server Security SQL Server Tips

Discover what clients are saying about Red9

Red9 has incredible expertise both in SQL migration and performance tuning.

The biggest benefit has been performance gains and tuning associated with migrating to AWS and a newer version of SQL Server with Always On clustering. Red9 was integral to this process. The deep knowledge of MSSQL and combined experience of Red9 have been a huge asset during a difficult migration. Red9 found inefficient indexes and performance bottlenecks that improved latency by over 400%.

Rich Staats 5 stars
Rich Staats
Cloud Engineer
MetalToad

Always willing to go an extra mile

Working with Red9 DBAs has been a pleasure. They are great team players and have an expert knowledge of SQL Server database administration. And are always willing to go the extra mile to get the project done.
5 stars
Evelyn A.
Sr. Database Administrator

Boosts server health and efficiency for enhanced customer satisfaction

Since adding Red9 to the reporting and DataWarehousing team, Red9 has done a good job coming up to speed on our environments and helping ensure we continue to meet our customer's needs. Red9 has taken ownership of our servers ensuring they remain healthy by monitoring and tuning inefficient queries.
5 stars
Andrew F.
Datawarehousing Manager
See more testimonials