SQL Server Error 1068: Only One List of Index Hints
In this article
- 1. Merge multiple hint lists into one
- 2. Remove redundant or conflicting hints
- 3. Use a single INDEX hint with multiple indexes
- 4. Replace index hints with query restructuring
- 5. Use FORCESEEK or FORCESCAN table hints
- 6. Apply hints at the query level with OPTION clause
- 7. Use plan guides for third-party queries
- 8. Check for accidental duplicate hints from dynamic SQL
- 9. Validate hint syntax with SET NOEXEC ON
- 10. Consult the execution plan for hint usage
- How to Choose
- Frequently Asked Questions
- Final Thoughts
SQL Server error 1068, "Only one list of index hints per table is allowed," occurs when a query specifies more than one index hint list for the same table. This list covers ten practical ways to fix or avoid the error, from rewriting the query to using plan guides. It is for database developers and DBAs who write or troubleshoot T-SQL queries and need clear, specific solutions.
1. Merge multiple hint lists into one
The most direct fix is to combine all index hints for a table into a single WITH clause. For example, instead of FROM Sales.Orders WITH (INDEX(IX_OrderDate)) WITH (INDEX(IX_CustomerID)), write FROM Sales.Orders WITH (INDEX(IX_OrderDate), INDEX(IX_CustomerID)). This satisfies the rule that only one list of index hints per table is allowed. It is worth doing because it preserves your intended indexes while eliminating the error immediately.
2. Remove redundant or conflicting hints
Review each hint and remove any that are unnecessary or contradictory. If you have both INDEX(IX_OrderDate) and INDEX(0) (a table scan hint), they conflict and cannot be combined. Keep only the hint that matches your performance goal. This is worth doing because extra hints can confuse the optimizer and degrade performance even after the syntax error is fixed.
3. Use a single INDEX hint with multiple indexes
The INDEX hint accepts a comma-separated list of index names or IDs. For instance, WITH (INDEX(IX_OrderDate, IX_CustomerID)) tells SQL Server to consider both indexes. This is valid and avoids multiple hint lists. It is worth using when you want the optimizer to choose between several indexes without forcing a specific one.
4. Replace index hints with query restructuring
Often, the need for multiple hints indicates a query that could be rewritten for better performance. For example, splitting a complex query into temp tables or using EXISTS instead of IN can lead to better plans without hints. This is worth considering because hints are fragile; schema changes can make them obsolete, and the optimizer may ignore them anyway.
5. Use FORCESEEK or FORCESCAN table hints
Instead of listing indexes, you can use the FORCESEEK or FORCESCAN hints to influence access methods without naming specific indexes. WITH (FORCESEEK) forces an index seek operation, which can be combined with other hints like INDEX. This is worth trying because it gives the optimizer flexibility to choose the best index while still enforcing a seek.
6. Apply hints at the query level with OPTION clause
Some hints, like MAXDOP or RECOMPILE, are specified in the OPTION clause at the end of the query. While index hints cannot be moved there, using query-level hints can sometimes reduce the need for table-level hints. This is worth knowing because it helps you separate performance tuning from table access hints.
7. Use plan guides for third-party queries
If you cannot modify the query text (e.g., it comes from a vendor application), you can use a plan guide to attach hints. Plan guides let you apply query hints without changing the application code. This is worth doing because it solves the error without touching the source, though it requires careful testing.
8. Check for accidental duplicate hints from dynamic SQL
When building dynamic SQL, string concatenation might inadvertently add the same hint twice. For example, a loop that appends WITH (INDEX(IX_OrderDate)) each iteration will produce multiple hint lists. This is worth checking because dynamic SQL is a common source of this error. Use a variable to track hints and add them only once.
9. Validate hint syntax with SET NOEXEC ON
Before running a query, you can use SET NOEXEC ON to compile but not execute it. This helps catch syntax errors like error 1068 without affecting data. This is worth doing because it speeds up development and prevents runtime failures.
10. Consult the execution plan for hint usage
After fixing the error, examine the actual execution plan to see if the hints are being used. Sometimes the optimizer ignores hints, and you may need to adjust your approach. This is worth doing because it confirms that your hints are effective and not causing unintended side effects.
How to Choose
When deciding which fix to use, consider the source of the query: if you control the code, merging hints (item 1) is simplest. For third-party queries, plan guides (item 7) are the way to go. If performance is poor, restructuring (item 4) or using FORCESEEK (item 5) may be better than forcing indexes. Always test changes in a non-production environment and monitor the impact on query plans. The goal is not just to eliminate the error but to ensure the query performs well and remains maintainable.
Frequently Asked Questions
What causes SQL Server error 1068?
It occurs when a query specifies more than one list of index hints for the same table, such as two separate WITH clauses each containing INDEX hints.
Can I use multiple indexes in a single hint?
Yes, the INDEX hint accepts a comma-separated list of index names or IDs, like WITH (INDEX(IX_OrderDate, IX_CustomerID)).
Are index hints always obeyed?
No, SQL Server may ignore hints if they are invalid or if the optimizer determines a better plan. Hints are not guaranteed.
How do I hint an index without naming it?
You can use FORCESEEK or FORCESCAN to influence access method without specifying an index name.
Can I apply hints to a query I cannot edit?
Yes, use a plan guide to attach hints to a query without changing its text.
Final Thoughts
Error 1068 is a syntax issue with a straightforward fix: ensure only one list of index hints per table. Beyond that, use hints sparingly and test their impact. The approaches above give you options whether you own the code or not, helping you maintain both correctness and performance.