How would you use query hints to influence the optimizer's behavior when you have determined the default execution plan is genuinely suboptimal?
Short answer
You can add specific query hints, such as forcing a particular join type, a specific index, or a maximum degree of parallelism, directly in your query using the OPTION clause, though this should be done carefully and only after confirming through testing that the hint genuinely…
Advertisement
AModel answer
You can add specific query hints, such as forcing a particular join type, a specific index, or a maximum degree of parallelism, directly in your query using the OPTION clause, though this should be done carefully and only after confirming through testing that the hint genuinely produces better performance, since hints can also prevent the optimizer from adapting to future changes in your data.
SELECT * FROM Orders o
JOIN Customers c ON o.CustomerId = c.CustomerId
OPTION (HASH JOIN);Real-world example
A database administrator forces a specific join type using a query hint after confirming through careful testing that the optimizer's default choice was consistently less efficient for this particular, unusually shaped query.
Likely follow-ups
- What are the risks of relying too heavily on query hints?
- How do you know when a query hint is genuinely still necessary after data or indexes have changed?
Related topics