- MySQL 5.1 Reference Manual :: 12 SQL Statement Syntax :: 12.2 Data Manipulation Statements :: 12.2.8 SELECT Syntax :: 12.2.8.2 Index Hint Syntax
-
- MySQL 5.1 Reference Manual
- Preface, Notes, Licenses
- 1 General Information
- 2 Installing and Upgrading MySQL
- 3 Tutorial
- 4 MySQL Programs
- 5 MySQL Server Administration
- 6 Backup and Recovery
- 7 Optimization
- 8 Language Structure
- 9 Internationalization and Localization
- 10 Data Types
- 11 Functions and Operators
- 12 SQL Statement Syntax
- 13 Storage Engines
- 14 High Availability and Scalability
- 15 MySQL Enterprise Monitor
- 16 Replication
- 17 MySQL Cluster NDB 6.X/7.X
- 18 Partitioning
- 19 Stored Programs and Views
- 20 INFORMATION_SCHEMA Tables
- 21 Connectors and APIs
- 22 Extending MySQL
- A MySQL 5.1 Frequently Asked Questions
- B Errors, Error Codes, and Common Problems
- C MySQL Change History
- D Restrictions and Limits
- Index
- Standard Index
- C Function Index
- Command Index
- Function Index
- INFORMATION_SCHEMA Index
- Transaction Isolation Level Index
- JOIN Types Index
- Operator Index
- Option Index
- Privileges Index
- SQL Modes Index
- Status Variable Index
- Statement/Syntax Index
- System Variable Index
You can provide hints to give the optimizer information about how to choose indexes during query processing. Section 12.2.8.1, “
JOIN
Syntax”, describes the general syntax for specifying tables in aSELECT
statement. The syntax for an individual table, including that for index hints, looks like this:tbl_name
[[AS]alias
] [index_hint_list
]index_hint_list
:index_hint
[,index_hint
] ...index_hint
: USE {INDEX|KEY} [{FOR {JOIN|ORDER BY|GROUP BY}] ([index_list
]) | IGNORE {INDEX|KEY} [{FOR {JOIN|ORDER BY|GROUP BY}] (index_list
) | FORCE {INDEX|KEY} [{FOR {JOIN|ORDER BY|GROUP BY}] (index_list
)index_list
:index_name
[,index_name
] ...By specifying
USE INDEX (
, you can tell MySQL to use only one of the named indexes to find rows in the table. The alternative syntaxindex_list
)IGNORE INDEX (
can be used to tell MySQL to not use some particular index or indexes. These hints are useful ifindex_list
)EXPLAIN
shows that MySQL is using the wrong index from the list of possible indexes.You can also use
FORCE INDEX
, which acts likeUSE INDEX (
but with the addition that a table scan is assumed to be very expensive. In other words, a table scan is used only if there is no way to use one of the given indexes to find rows in the table.index_list
)Each hint requires the names of indexes, not the names of columns. The name of a
PRIMARY KEY
isPRIMARY
. To see the index names for a table, useSHOW INDEX
.An
index_name
value need not be a full index name. It can be an unambiguous prefix of an index name. If a prefix is ambiguous, an error occurs.Prior to MySQL 5.1.17,
USE INDEX
,IGNORE INDEX
, andFORCE INDEX
affect only which indexes are used when MySQL decides how to find rows in the table and how to process joins. They do not affect whether an index is used when resolving anORDER BY
orGROUP BY
clause.Examples:
SELECT * FROM table1 USE INDEX (col1_index,col2_index) WHERE col1=1 AND col2=2 AND col3=3; SELECT * FROM table1 IGNORE INDEX (col3_index) WHERE col1=1 AND col2=2 AND col3=3;
As of MySQL 5.1.17, the syntax for index hints is extended in the following ways:
It is syntactically valid to specify an empty
index_list
forUSE INDEX
, which means “use no indexes.” Specifying an emptyindex_list
forFORCE INDEX
orIGNORE INDEX
is a syntax error.You can specify the scope of a index hint by adding a
FOR
clause to the hint. This provides more fine-grained control over the optimizer's selection of an execution plan for various phases of query processing. To affect only the indexes used when MySQL decides how to find rows in the table and how to process joins, useFOR JOIN
. To influence index usage for sorting or grouping rows, useFOR ORDER BY
orFOR GROUP BY
. (However, if there is a covering index for the table and it is used to access the table, the optimizer will ignoreIGNORE INDEX FOR {ORDER BY|GROUP BY}
hints that disable that index.)-
You can specify multiple index hints:
SELECT * FROM t1 USE INDEX (i1) IGNORE INDEX FOR ORDER BY (i2) ORDER BY a;
It is not a error to name the same index in several hints (even within the same hint):
SELECT * FROM t1 USE INDEX (i1) USE INDEX (i1,i1);
However, it is an error to mix
USE INDEX
andFORCE INDEX
for the same table:SELECT * FROM t1 USE INDEX FOR JOIN (i1) FORCE INDEX FOR JOIN (i2);
The default scope of index hints also is changed as of MySQL 5.1.17. Formerly, index hints applied only to how indexes are used for retrieval of records and not during resolution of
ORDER BY
orGROUP BY
clauses. As of 5.1.17, if you specify noFOR
clause for an index hint, the hint by default applies to all parts of the statement. For example, this hint:IGNORE INDEX (i1)
is equivalent to this combination of hints:
IGNORE INDEX FOR JOIN (i1) IGNORE INDEX FOR ORDER BY (i1) IGNORE INDEX FOR GROUP BY (i1)
To cause the server to use the older behavior for hint scope when no
FOR
clause is present (so that hints apply only to row retrieval), enable theold
system variable at server startup. Take care about enabling this variable in a replication setup. With statement-based binary logging, having different modes for the master and slaves might lead to replication errors.When index hints are processed, they are collected in a single list by type (
USE
,FORCE
,IGNORE
) and by scope (FOR JOIN
,FOR ORDER BY
,FOR GROUP BY
). For example:SELECT * FROM t1 USE INDEX () IGNORE INDEX (i2) USE INDEX (i1) USE INDEX (i2);
is equivalent to:
SELECT * FROM t1 USE INDEX (i1,i2) IGNORE INDEX (i2);
The index hints then are applied for each scope in the following order:
{USE|FORCE} INDEX
is applied if present. (If not, the optimizer-determined set of indexes is used.)-
IGNORE INDEX
is applied over the result of the previous step. For example, the following two queries are equivalent:SELECT * FROM t1 USE INDEX (i1) IGNORE INDEX (i2) USE INDEX (i2); SELECT * FROM t1 USE INDEX (i1);
For
FULLTEXT
searches, index hints do not work before MySQL 5.1.31. As of 5.1.31, index hints work as follows:-
For natural language mode searches, index hints are silently ignored. For example,
IGNORE INDEX(i)
is ignored with no warning and the index is still used.For boolean mode searches, index hints with
FOR ORDER BY
orFOR GROUP BY
are silently ignored. Index hints withFOR JOIN
or noFOR
modifier are honored. In contrast to how hints apply for non-FULLTEXT
searches, the hint is used for all phases of query execution (finding rows and retrieval, grouping, and ordering). This is true even if the hint is given for a non-FULLTEXT
index.
For example, the following two queries are equivalent:
SELECT * FROM t USE INDEX (index1) IGNORE INDEX (index1) FOR ORDER BY IGNORE INDEX (index1) FOR GROUP BY WHERE ... IN BOOLEAN MODE ... ; SELECT * FROM t USE INDEX (index1) WHERE ... IN BOOLEAN MODE ... ;
Index hints are accepted but ignored for
UPDATE
statements.