Warning
Error Code: 3126

MySQL Error 3126: Conflicting or Duplicate Query Hints

📦 MySQL
📋

Description

MySQL Error 3126 indicates that a query hint provided in an SQL statement is being ignored because it conflicts with another hint or is duplicated. This means the database optimizer has detected an ambiguous or redundant instruction regarding query execution strategy, and will proceed by disregarding the problematic hint.
💬

Error Message

Hint %s is ignored as conflicting/duplicated
🔍

Known Causes

3 known causes
⚠️
Duplicate Query Hints
The same query hint (e.g., specifying 'USE INDEX' for the same table twice) has been included multiple times within a single SQL statement.
⚠️
Conflicting Hint Directives
The SQL query contains hints that are logically contradictory or mutually exclusive, such as using 'USE INDEX' and 'IGNORE INDEX' for the same table in the same query.
⚠️
Overlapping Hint Scope
Hints are applied that, while not directly conflicting in syntax, target the same optimization aspect in a way that one hint supersedes or makes another redundant.
🛠️

Solutions

3 solutions available

1. Remove Duplicate or Conflicting Query Hints easy

Identify and eliminate redundant or contradictory hints within your SQL query.

1
Examine the SQL query that is generating the error. Look for instances where the same hint is applied multiple times or where different hints for the same operation are present.
SELECT /*+ INDEX(t1 idx_col1) */ /*+ INDEX(t1 idx_col2) */ col1 FROM table1 t1 WHERE col1 = 1;
2
Remove any duplicate hints. For example, if `INDEX(t1 idx_col1)` appears twice, delete one of them.
SELECT /*+ INDEX(t1 idx_col1) */ col1 FROM table1 t1 WHERE col1 = 1;
3
Resolve conflicting hints. For instance, if you have `USE_INDEX` and `IGNORE_INDEX` for the same table and column, choose one that aligns with your optimization goal.
SELECT /*+ USE_INDEX(t1 idx_col1) */ /*+ IGNORE_INDEX(t1 idx_col1) */ col1 FROM table1 t1 WHERE col1 = 1;
4
Apply the corrected query.
SELECT /*+ USE_INDEX(t1 idx_col1) */ col1 FROM table1 t1 WHERE col1 = 1;

2. Consolidate Similar Hints easy

Combine multiple similar hints into a single, more efficient hint.

1
Identify if you have multiple hints that achieve a similar optimization goal, especially for different parts of the same table or index.
SELECT /*+ INDEX(t1 idx_col1) */ /*+ INDEX(t1 idx_col2) */ col1 FROM table1 t1 WHERE col1 = 1;
2
Check the MySQL documentation for the specific hint you are using to see if it supports multiple arguments or a consolidated form.
text: Refer to MySQL documentation for specific hint syntax, e.g., `EXPLAIN` output or online manuals.
3
Modify the query to use a single hint that encompasses the desired optimization. For example, if you want to suggest using an index, and the index is on multiple columns, you might be able to specify the index name once.
SELECT /*+ INDEX(t1 my_composite_index) */ col1 FROM table1 t1 WHERE col1 = 1 AND col2 = 2;
4
Apply the consolidated hint.
SELECT /*+ INDEX(t1 my_composite_index) */ col1 FROM table1 t1 WHERE col1 = 1 AND col2 = 2;

3. Review and Simplify Query Logic medium

Re-evaluate the query's structure to see if hints are truly necessary or if the query can be rewritten for better performance.

1
Run `EXPLAIN` on the query to understand the current execution plan and identify any performance bottlenecks.
EXPLAIN SELECT /*+ INDEX(t1 idx_col1) */ col1 FROM table1 t1 WHERE col1 = 1;
2
Analyze the `EXPLAIN` output. If the optimizer is already choosing the desired index or execution path, the hints might be redundant and could be removed.
text: Observe the 'key' and 'type' columns in the EXPLAIN output. If they already reflect the intended optimization, the hint might be unnecessary.
3
Consider rewriting subqueries, using joins more effectively, or simplifying complex `WHERE` clauses to naturally guide the optimizer without explicit hints.
text: Example: Instead of a subquery, use a JOIN if it leads to a clearer execution plan.
4
Test the query without hints to confirm that the performance is still acceptable or has improved.
SELECT col1 FROM table1 t1 WHERE col1 = 1;
🔗

Related Errors

5 related errors