Documentation Home
MySQL 8.0 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 43.3Mb
PDF (A4) - 43.4Mb
Man Pages (TGZ) - 297.1Kb
Man Pages (Zip) - 402.3Kb
Info (Gzip) - 4.3Mb
Info (Zip) - 4.3Mb
Excerpts from this Manual

10.9.3 Optimizer Hints

One means of control over optimizer strategies is to set theoptimizer_switch system variable (seeSection 10.9.2, “Switchable Optimizations”). Changes to this variable affect execution of all subsequent queries; to affect one query differently from another, it is necessary to changeoptimizer_switch before each one.

Another way to control the optimizer is by using optimizer hints, which can be specified within individual statements. Because optimizer hints apply on a per-statement basis, they provide finer control over statement execution plans than can be achieved usingoptimizer_switch. For example, you can enable an optimization for one table in a statement and disable the optimization for a different table. Hints within a statement take precedence overoptimizer_switch flags.

Examples:

SELECT /*+ NO_RANGE_OPTIMIZATION(t3 PRIMARY, f2_idx) */ f1  FROM t3 WHERE f1 > 30 AND f1 < 33;SELECT /*+ BKA(t1) NO_BKA(t2) */ * FROM t1 INNER JOIN t2 WHERE ...;SELECT /*+ NO_ICP(t1, t2) */ * FROM t1 INNER JOIN t2 WHERE ...;SELECT /*+ SEMIJOIN(FIRSTMATCH, LOOSESCAN) */ * FROM t1 ...;EXPLAIN SELECT /*+ NO_ICP(t1) */ * FROM t1 WHERE ...;SELECT /*+ MERGE(dt) */ * FROM (SELECT * FROM t1) AS dt;INSERT /*+ SET_VAR(foreign_key_checks=OFF) */ INTO t2 VALUES(2);

Optimizer hints, described here, differ from index hints, described inSection 10.9.4, “Index Hints”. Optimizer and index hints may be used separately or together.

Optimizer Hint Overview

Optimizer hints apply at different scope levels:

  • Global: The hint affects the entire statement

  • Query block: The hint affects a particular query block within a statement

  • Table-level: The hint affects a particular table within a query block

  • Index-level: The hint affects a particular index within a table

The following table summarizes the available optimizer hints, the optimizer strategies they affect, and the scope or scopes at which they apply. More details are given later.

Table 10.2 Optimizer Hints Available

Hint NameDescriptionApplicable Scopes
BKA,NO_BKAAffects Batched Key Access join processingQuery block, table
BNL,NO_BNLPrior to MySQL 8.0.20: affects Block Nested-Loop join processing; MySQL 8.0.18 and later: also affects hash join optimization; MySQL 8.0.20 and later: affects hash join optimization onlyQuery block, table
DERIVED_CONDITION_PUSHDOWN,NO_DERIVED_CONDITION_PUSHDOWNUse or ignore the derived condition pushdown optimization for materialized derived tables (Added in MySQL 8.0.22)Query block, table
GROUP_INDEX,NO_GROUP_INDEXUse or ignore the specified index or indexes for index scans inGROUP BY operations (Added in MySQL 8.0.20)Index
HASH_JOIN,NO_HASH_JOINAffects Hash Join optimization (MySQL 8.0.18 onlyQuery block, table
INDEX,NO_INDEXActs as the combination ofJOIN_INDEX,GROUP_INDEX, andORDER_INDEX, or as the combination ofNO_JOIN_INDEX,NO_GROUP_INDEX, andNO_ORDER_INDEX (Added in MySQL 8.0.20)Index
INDEX_MERGE,NO_INDEX_MERGEAffects Index Merge optimizationTable, index
JOIN_FIXED_ORDERUse table order specified inFROM clause for join orderQuery block
JOIN_INDEX,NO_JOIN_INDEXUse or ignore the specified index or indexes for any access method (Added in MySQL 8.0.20)Index
JOIN_ORDERUse table order specified in hint for join orderQuery block
JOIN_PREFIXUse table order specified in hint for first tables of join orderQuery block
JOIN_SUFFIXUse table order specified in hint for last tables of join orderQuery block
MAX_EXECUTION_TIMELimits statement execution timeGlobal
MERGE,NO_MERGEAffects derived table/view merging into outer query blockTable
MRR,NO_MRRAffects Multi-Range Read optimizationTable, index
NO_ICPAffects Index Condition Pushdown optimizationTable, index
NO_RANGE_OPTIMIZATIONAffects range optimizationTable, index
ORDER_INDEX,NO_ORDER_INDEXUse or ignore the specified index or indexes for sorting rows (Added in MySQL 8.0.20)Index
QB_NAMEAssigns name to query blockQuery block
RESOURCE_GROUPSet resource group during statement executionGlobal
SEMIJOIN,NO_SEMIJOINAffects semijoin strategies; beginning with MySQL 8.0.17, this also applies to antijoinsQuery block
SKIP_SCAN,NO_SKIP_SCANAffects Skip Scan optimizationTable, index
SET_VARSet variable during statement executionGlobal
SUBQUERYAffects materialization,IN-to-EXISTS subquery strategiesQuery block

Disabling an optimization prevents the optimizer from using it. Enabling an optimization means the optimizer is free to use the strategy if it applies to statement execution, not that the optimizer necessarily uses it.

Optimizer Hint Syntax

MySQL supports comments in SQL statements as described inSection 11.7, “Comments”. Optimizer hints must be specified within/*+ ... */ comments. That is, optimizer hints use a variant of/* ... */ C-style comment syntax, with a+ character following the/* comment opening sequence. Examples:

/*+ BKA(t1) *//*+ BNL(t1, t2) *//*+ NO_RANGE_OPTIMIZATION(t4 PRIMARY) *//*+ QB_NAME(qb2) */

Whitespace is permitted after the+ character.

The parser recognizes optimizer hint comments after the initial keyword ofSELECT,UPDATE,INSERT,REPLACE, andDELETE statements. Hints are permitted in these contexts:

  • At the beginning of query and data change statements:

    SELECT /*+ ... */ ...INSERT /*+ ... */ ...REPLACE /*+ ... */ ...UPDATE /*+ ... */ ...DELETE /*+ ... */ ...
  • At the beginning of query blocks:

    (SELECT /*+ ... */ ... )(SELECT ... ) UNION (SELECT /*+ ... */ ... )(SELECT /*+ ... */ ... ) UNION (SELECT /*+ ... */ ... )UPDATE ... WHERE x IN (SELECT /*+ ... */ ...)INSERT ... SELECT /*+ ... */ ...
  • In hintable statements prefaced byEXPLAIN. For example:

    EXPLAIN SELECT /*+ ... */ ...EXPLAIN UPDATE ... WHERE x IN (SELECT /*+ ... */ ...)

    The implication is that you can useEXPLAIN to see how optimizer hints affect execution plans. UseSHOW WARNINGS immediately afterEXPLAIN to see how hints are used. The extendedEXPLAIN output displayed by a followingSHOW WARNINGS indicates which hints were used. Ignored hints are not displayed.

A hint comment may contain multiple hints, but a query block cannot contain multiple hint comments. This is valid:

SELECT /*+ BNL(t1) BKA(t2) */ ...

But this is invalid:

SELECT /*+ BNL(t1) */ /* BKA(t2) */ ...

When a hint comment contains multiple hints, the possibility of duplicates and conflicts exists. The following general guidelines apply. For specific hint types, additional rules may apply, as indicated in the hint descriptions.

  • Duplicate hints: For a hint such as/*+ MRR(idx1) MRR(idx1) */, MySQL uses the first hint and issues a warning about the duplicate hint.

  • Conflicting hints: For a hint such as/*+ MRR(idx1) NO_MRR(idx1) */, MySQL uses the first hint and issues a warning about the second conflicting hint.

Query block names are identifiers and follow the usual rules about what names are valid and how to quote them (seeSection 11.2, “Schema Object Names”).

Hint names, query block names, and strategy names are not case-sensitive. References to table and index names follow the usual identifier case-sensitivity rules (seeSection 11.2.3, “Identifier Case Sensitivity”).

Join-Order Optimizer Hints

Join-order hints affect the order in which the optimizer joins tables.

Syntax of theJOIN_FIXED_ORDER hint:

hint_name([@query_block_name])

Syntax of other join-order hints:

hint_name([@query_block_name]tbl_name [,tbl_name] ...)hint_name(tbl_name[@query_block_name] [,tbl_name[@query_block_name]] ...)

The syntax refers to these terms:

  • hint_name: These hint names are permitted:

    • JOIN_FIXED_ORDER: Force the optimizer to join tables using the order in which they appear in theFROM clause. This is the same as specifyingSELECT STRAIGHT_JOIN.

    • JOIN_ORDER: Instruct the optimizer to join tables using the specified table order. The hint applies to the named tables. The optimizer may place tables that are not named anywhere in the join order, including between specified tables.

    • JOIN_PREFIX: Instruct the optimizer to join tables using the specified table order for the first tables of the join execution plan. The hint applies to the named tables. The optimizer places all other tables after the named tables.

    • JOIN_SUFFIX: Instruct the optimizer to join tables using the specified table order for the last tables of the join execution plan. The hint applies to the named tables. The optimizer places all other tables before the named tables.

  • tbl_name: The name of a table used in the statement. A hint that names tables applies to all tables that it names. TheJOIN_FIXED_ORDER hint names no tables and applies to all tables in theFROM clause of the query block in which it occurs.

    If a table has an alias, hints must refer to the alias, not the table name.

    Table names in hints cannot be qualified with schema names.

  • query_block_name: The query block to which the hint applies. If the hint includes no leading@query_block_name, the hint applies to the query block in which it occurs. Fortbl_name@query_block_name syntax, the hint applies to the named table in the named query block. To assign a name to a query block, seeOptimizer Hints for Naming Query Blocks.

Example:

SELECT/*+ JOIN_PREFIX(t2, t5@subq2, t4@subq1)    JOIN_ORDER(t4@subq1, t3)    JOIN_SUFFIX(t1) */COUNT(*) FROM t1 JOIN t2 JOIN t3           WHERE t1.f1 IN (SELECT /*+ QB_NAME(subq1) */ f1 FROM t4)             AND t2.f1 IN (SELECT /*+ QB_NAME(subq2) */ f1 FROM t5);

Hints control the behavior of semijoin tables that are merged to the outer query block. If subqueriessubq1 andsubq2 are converted to semijoins, tablest4@subq1 andt5@subq2 are merged to the outer query block. In this case, the hint specified in the outer query block controls the behavior oft4@subq1,t5@subq2 tables.

The optimizer resolves join-order hints according to these principles:

  • Multiple hint instances

    Only oneJOIN_PREFIX andJOIN_SUFFIX hint of each type are applied. Any later hints of the same type are ignored with a warning.JOIN_ORDER can be specified several times.

    Examples:

    /*+ JOIN_PREFIX(t1) JOIN_PREFIX(t2) */

    The secondJOIN_PREFIX hint is ignored with a warning.

    /*+ JOIN_PREFIX(t1) JOIN_SUFFIX(t2) */

    Both hints are applicable. No warning occurs.

    /*+ JOIN_ORDER(t1, t2) JOIN_ORDER(t2, t3) */

    Both hints are applicable. No warning occurs.

  • Conflicting hints

    In some cases hints can conflict, such as whenJOIN_ORDER andJOIN_PREFIX have table orders that are impossible to apply at the same time:

    SELECT /*+ JOIN_ORDER(t1, t2) JOIN_PREFIX(t2, t1) */ ... FROM t1, t2;

    In this case, the first specified hint is applied and subsequent conflicting hints are ignored with no warning. A valid hint that is impossible to apply is silently ignored with no warning.

  • Ignored hints

    A hint is ignored if a table specified in the hint has a circular dependency.

    Example:

    /*+ JOIN_ORDER(t1, t2) JOIN_PREFIX(t2, t1) */

    TheJOIN_ORDER hint sets tablet2 dependent ont1. TheJOIN_PREFIX hint is ignored because tablet1 cannot be dependent ont2. Ignored hints are not displayed in extendedEXPLAIN output.

  • Interaction withconst tables

    The MySQL optimizer placesconst tables first in the join order, and the position of aconst table cannot be affected by hints. References toconst tables in join-order hints are ignored, although the hint is still applicable. For example, these are equivalent:

    JOIN_ORDER(t1,const_tbl, t2)JOIN_ORDER(t1, t2)

    Accepted hints shown in extendedEXPLAIN output includeconst tables as they were specified.

  • Interaction with types of join operations

    MySQL supports several type of joins:LEFT,RIGHT,INNER,CROSS,STRAIGHT_JOIN. A hint that conflicts with the specified type of join is ignored with no warning.

    Example:

    SELECT /*+ JOIN_PREFIX(t1, t2) */FROM t2 LEFT JOIN t1;

    Here a conflict occurs between the requested join order in the hint and the order required by theLEFT JOIN. The hint is ignored with no warning.

Table-Level Optimizer Hints

Table-level hints affect:

These hint types apply to specific tables, or all tables in a query block.

Syntax of table-level hints:

hint_name([@query_block_name] [tbl_name [,tbl_name] ...])hint_name([tbl_name@query_block_name [,tbl_name@query_block_name] ...])

The syntax refers to these terms:

  • hint_name: These hint names are permitted:

    • BKA,NO_BKA: Enable or disable batched key access for the specified tables.

    • BNL,NO_BNL: Enable or disable block nested loop for the specified tables. In MySQL 8.0.18 and later, these hints also enable and disable the hash join optimization.

      Note

      The block-nested loop optimization is removed in MySQL 8.0.20 and later releases, butBNL andNO_BNL continue to be supported for enabling and disabling hash joins.

    • DERIVED_CONDITION_PUSHDOWN,NO_DERIVED_CONDITION_PUSHDOWN: Enable or disable use of derived table condition pushdown for the specified tables (added in MySQL 8.0.22). For more information, seeSection 10.2.2.5, “Derived Condition Pushdown Optimization”.

    • HASH_JOIN,NO_HASH_JOIN: In MySQL 8.0.18 only, enable or disable use of a hash join for the specified tables. These hints have no effect in MySQL 8.0.19 or later, where you should useBNL orNO_BNL instead.

    • MERGE,NO_MERGE: Enable merging for the specified tables, view references or common table expressions; or disable merging and use materialization instead.

    Note

    To use a block nested loop or batched key access hint to enable join buffering for any inner table of an outer join, join buffering must be enabled for all inner tables of the outer join.

  • tbl_name: The name of a table used in the statement. The hint applies to all tables that it names. If the hint names no tables, it applies to all tables of the query block in which it occurs.

    If a table has an alias, hints must refer to the alias, not the table name.

    Table names in hints cannot be qualified with schema names.

  • query_block_name: The query block to which the hint applies. If the hint includes no leading@query_block_name, the hint applies to the query block in which it occurs. Fortbl_name@query_block_name syntax, the hint applies to the named table in the named query block. To assign a name to a query block, seeOptimizer Hints for Naming Query Blocks.

Examples:

SELECT /*+ NO_BKA(t1, t2) */ t1.* FROM t1 INNER JOIN t2 INNER JOIN t3;SELECT /*+ NO_BNL() BKA(t1) */ t1.* FROM t1 INNER JOIN t2 INNER JOIN t3;SELECT /*+ NO_MERGE(dt) */ * FROM (SELECT * FROM t1) AS dt;

A table-level hint applies to tables that receive records from previous tables, not sender tables. Consider this statement:

SELECT /*+ BNL(t2) */ FROM t1, t2;

If the optimizer chooses to processt1 first, it applies a Block Nested-Loop join tot2 by buffering the rows fromt1 before starting to read fromt2. If the optimizer instead chooses to processt2 first, the hint has no effect becauset2 is a sender table.

For theMERGE andNO_MERGE hints, these precedence rules apply:

  • A hint takes precedence over any optimizer heuristic that is not a technical constraint. (If providing a hint as a suggestion has no effect, the optimizer has a reason for ignoring it.)

  • A hint takes precedence over thederived_merge flag of theoptimizer_switch system variable.

  • For view references, anALGORITHM={MERGE|TEMPTABLE} clause in the view definition takes precedence over a hint specified in the query referencing the view.

Index-Level Optimizer Hints

Index-level hints affect which index-processing strategies the optimizer uses for particular tables or indexes. These hint types affect use of Index Condition Pushdown (ICP), Multi-Range Read (MRR), Index Merge, and range optimizations (seeSection 10.2.1, “Optimizing SELECT Statements”).

Syntax of index-level hints:

hint_name([@query_block_name]tbl_name [index_name [,index_name] ...])hint_name(tbl_name@query_block_name [index_name [,index_name] ...])

The syntax refers to these terms:

  • hint_name: These hint names are permitted:

    • GROUP_INDEX,NO_GROUP_INDEX: Enable or disable the specified index or indexes for index scans forGROUP BY operations. Equivalent to the index hintsFORCE INDEX FOR GROUP BY,IGNORE INDEX FOR GROUP BY. Available in MySQL 8.0.20 and later.

    • INDEX,NO_INDEX: Acts as the combination ofJOIN_INDEX,GROUP_INDEX, andORDER_INDEX, forcing the server to use the specified index or indexes for any and all scopes, or as the combination ofNO_JOIN_INDEX,NO_GROUP_INDEX, andNO_ORDER_INDEX, which causes the server to ignore the specified index or indexes for any and all scopes. Equivalent toFORCE INDEX,IGNORE INDEX. Available beginning with MySQL 8.0.20.

    • INDEX_MERGE,NO_INDEX_MERGE: Enable or disable the Index Merge access method for the specified table or indexes. For information about this access method, seeSection 10.2.1.3, “Index Merge Optimization”. These hints apply to all three Index Merge algorithms.

      TheINDEX_MERGE hint forces the optimizer to use Index Merge for the specified table using the specified set of indexes. If no index is specified, the optimizer considers all possible index combinations and selects the least expensive one. The hint may be ignored if the index combination is inapplicable to the given statement.

      TheNO_INDEX_MERGE hint disables Index Merge combinations that involve any of the specified indexes. If the hint specifies no indexes, Index Merge is not permitted for the table.

    • JOIN_INDEX,NO_JOIN_INDEX: Forces MySQL to use or ignore the specified index or indexes for any access method, such asref,range,index_merge, and so on. Equivalent toFORCE INDEX FOR JOIN,IGNORE INDEX FOR JOIN. Available in MySQL 8.0.20 and later.

    • MRR,NO_MRR: Enable or disable MRR for the specified table or indexes. MRR hints apply only toInnoDB andMyISAM tables. For information about this access method, seeSection 10.2.1.11, “Multi-Range Read Optimization”.

    • NO_ICP: Disable ICP for the specified table or indexes. By default, ICP is a candidate optimization strategy, so there is no hint for enabling it. For information about this access method, seeSection 10.2.1.6, “Index Condition Pushdown Optimization”.

    • NO_RANGE_OPTIMIZATION: Disable index range access for the specified table or indexes. This hint also disables Index Merge and Loose Index Scan for the table or indexes. By default, range access is a candidate optimization strategy, so there is no hint for enabling it.

      This hint may be useful when the number of ranges may be high and range optimization would require many resources.

    • ORDER_INDEX,NO_ORDER_INDEX: Cause MySQL to use or to ignore the specified index or indexes for sorting rows. Equivalent toFORCE INDEX FOR ORDER BY,IGNORE INDEX FOR ORDER BY. Available beginning with MySQL 8.0.20.

    • SKIP_SCAN,NO_SKIP_SCAN: Enable or disable the Skip Scan access method for the specified table or indexes. For information about this access method, seeSkip Scan Range Access Method. These hints are available as of MySQL 8.0.13.

      TheSKIP_SCAN hint forces the optimizer to use Skip Scan for the specified table using the specified set of indexes. If no index is specified, the optimizer considers all possible indexes and selects the least expensive one. The hint may be ignored if the index is inapplicable to the given statement.

      TheNO_SKIP_SCAN hint disables Skip Scan for the specified indexes. If the hint specifies no indexes, Skip Scan is not permitted for the table.

  • tbl_name: The table to which the hint applies.

  • index_name: The name of an index in the named table. The hint applies to all indexes that it names. If the hint names no indexes, it applies to all indexes in the table.

    To refer to a primary key, use the namePRIMARY. To see the index names for a table, useSHOW INDEX.

  • query_block_name: The query block to which the hint applies. If the hint includes no leading@query_block_name, the hint applies to the query block in which it occurs. Fortbl_name@query_block_name syntax, the hint applies to the named table in the named query block. To assign a name to a query block, seeOptimizer Hints for Naming Query Blocks.

Examples:

SELECT /*+ INDEX_MERGE(t1 f3, PRIMARY) */ f2 FROM t1  WHERE f1 = 'o' AND f2 = f3 AND f3 <= 4;SELECT /*+ MRR(t1) */ * FROM t1 WHERE f2 <= 3 AND 3 <= f3;SELECT /*+ NO_RANGE_OPTIMIZATION(t3 PRIMARY, f2_idx) */ f1  FROM t3 WHERE f1 > 30 AND f1 < 33;INSERT INTO t3(f1, f2, f3)  (SELECT /*+ NO_ICP(t2) */ t2.f1, t2.f2, t2.f3 FROM t1,t2   WHERE t1.f1=t2.f1 AND t2.f2 BETWEEN t1.f1   AND t1.f2 AND t2.f2 + 1 >= t1.f1 + 1);SELECT /*+ SKIP_SCAN(t1 PRIMARY) */ f1, f2  FROM t1 WHERE f2 > 40;

The following examples use the Index Merge hints, but other index-level hints follow the same principles regarding hint ignoring and precedence of optimizer hints in relation to theoptimizer_switch system variable or index hints.

Assume that tablet1 has columnsa,b,c, andd; and that indexes namedi_a,i_b, andi_c exist ona,b, andc, respectively:

SELECT /*+ INDEX_MERGE(t1 i_a, i_b, i_c)*/ * FROM t1  WHERE a = 1 AND b = 2 AND c = 3 AND d = 4;

Index Merge is used for(i_a, i_b, i_c) in this case.

SELECT /*+ INDEX_MERGE(t1 i_a, i_b, i_c)*/ * FROM t1  WHERE b = 1 AND c = 2 AND d = 3;

Index Merge is used for(i_b, i_c) in this case.

/*+ INDEX_MERGE(t1 i_a, i_b) NO_INDEX_MERGE(t1 i_b) */

NO_INDEX_MERGE is ignored because there is a preceding hint for the same table.

/*+ NO_INDEX_MERGE(t1 i_a, i_b) INDEX_MERGE(t1 i_b) */

INDEX_MERGE is ignored because there is a preceding hint for the same table.

For theINDEX_MERGE andNO_INDEX_MERGE optimizer hints, these precedence rules apply:

  • If an optimizer hint is specified and is applicable, it takes precedence over the Index Merge-related flags of theoptimizer_switch system variable.

    SET optimizer_switch='index_merge_intersection=off';SELECT /*+ INDEX_MERGE(t1 i_b, i_c) */ * FROM t1WHERE b = 1 AND c = 2 AND d = 3;

    The hint takes precedence overoptimizer_switch. Index Merge is used for(i_b, i_c) in this case.

    SET optimizer_switch='index_merge_intersection=on';SELECT /*+ INDEX_MERGE(t1 i_b) */ * FROM t1WHERE b = 1 AND c = 2 AND d = 3;

    The hint specifies only one index, so it is inapplicable, and theoptimizer_switch flag (on) applies. Index Merge is used if the optimizer assesses it to be cost efficient.

    SET optimizer_switch='index_merge_intersection=off';SELECT /*+ INDEX_MERGE(t1 i_b) */ * FROM t1WHERE b = 1 AND c = 2 AND d = 3;

    The hint specifies only one index, so it is inapplicable, and theoptimizer_switch flag (off) applies. Index Merge is not used.

  • The index-level optimizer hintsGROUP_INDEX,INDEX,JOIN_INDEX, andORDER_INDEX all take precedence over the equivalentFORCE INDEX hints; that is, they cause theFORCE INDEX hints to be ignored. Likewise, theNO_GROUP_INDEX,NO_INDEX,NO_JOIN_INDEX, andNO_ORDER_INDEX hints all take precedence over anyIGNORE INDEX equivalents, also causing them to be ignored.

    The index-level optimizer hintsGROUP_INDEX,NO_GROUP_INDEX,INDEX,NO_INDEX,JOIN_INDEX,NO_JOIN_INDEX,ORDER_INDEX, andNO_ORDER_INDEX hints all take precedence over all other optimizer hints, including other index-level optimizer hints. Any other optimizer hints are applied only to the indexes permitted by these.

    TheGROUP_INDEX,INDEX,JOIN_INDEX, andORDER_INDEX hints are all equivalent toFORCE INDEX and not toUSE INDEX. This is because using one or more of these hints means that a table scan is used only if there is no way to use one of the named indexes to find rows in the table. To cause MySQL to use the same index or set of indexes as with a given instance ofUSE INDEX, you can useNO_INDEX,NO_JOIN_INDEX,NO_GROUP_INDEX,NO_ORDER_INDEX, or some combination of these.

    To replicate the effect thatUSE INDEX has in the querySELECT a,c FROM t1 USE INDEX FOR ORDER BY (i_a) ORDER BY a, you can use theNO_ORDER_INDEX optimizer hint to cover all indexes on the table except the one that is desired like this:

    SELECT /*+ NO_ORDER_INDEX(t1 i_b,i_c) */ a,c    FROM t1    ORDER BY a;

    Attempting to combineNO_ORDER_INDEX for the table as a whole withUSE INDEX FOR ORDER BY does not work to do this, becauseNO_ORDER_BY causesUSE INDEX to be ignored, as shown here:

    mysql> EXPLAIN SELECT /*+ NO_ORDER_INDEX(t1) */ a,c FROM t1    ->     USE INDEX FOR ORDER BY (i_a) ORDER BY a\G*************************** 1. row ***************************           id: 1  select_type: SIMPLE        table: t1   partitions: NULL         type: ALLpossible_keys: NULL          key: NULL      key_len: NULL          ref: NULL         rows: 256     filtered: 100.00        Extra: Using filesort
  • TheUSE INDEX,FORCE INDEX, andIGNORE INDEX index hints have higher priority than theINDEX_MERGE andNO_INDEX_MERGE optimizer hints.

    /*+ INDEX_MERGE(t1 i_a, i_b, i_c) */ ... IGNORE INDEX i_a

    IGNORE INDEX takes precedence overINDEX_MERGE, so indexi_a is excluded from the possible ranges for Index Merge.

    /*+ NO_INDEX_MERGE(t1 i_a, i_b) */ ... FORCE INDEX i_a, i_b

    Index Merge is disallowed fori_a, i_b because ofFORCE INDEX, but the optimizer is forced to use eitheri_a ori_b forrange orref access. There are no conflicts; both hints are applicable.

  • If anIGNORE INDEX hint names multiple indexes, those indexes are unavailable for Index Merge.

  • TheFORCE INDEX andUSE INDEX hints make only the named indexes to be available for Index Merge.

    SELECT /*+ INDEX_MERGE(t1 i_a, i_b, i_c) */ a FROM t1FORCE INDEX (i_a, i_b) WHERE c = 'h' AND a = 2 AND b = 'b';

    The Index Merge intersection access algorithm is used for(i_a, i_b). The same is true ifFORCE INDEX is changed toUSE INDEX.

Subquery Optimizer Hints

Subquery hints affect whether to use semijoin transformations and which semijoin strategies to permit, and, when semijoins are not used, whether to use subquery materialization orIN-to-EXISTS transformations. For more information about these optimizations, seeSection 10.2.2, “Optimizing Subqueries, Derived Tables, View References, and Common Table Expressions”.

Syntax of hints that affect semijoin strategies:

hint_name([@query_block_name] [strategy [,strategy] ...])

The syntax refers to these terms:

  • hint_name: These hint names are permitted:

  • strategy: A semijoin strategy to be enabled or disabled. These strategy names are permitted:DUPSWEEDOUT,FIRSTMATCH,LOOSESCAN,MATERIALIZATION.

    ForSEMIJOIN hints, if no strategies are named, semijoin is used if possible based on the strategies enabled according to theoptimizer_switch system variable. If strategies are named but inapplicable for the statement,DUPSWEEDOUT is used.

    ForNO_SEMIJOIN hints, if no strategies are named, semijoin is not used. If strategies are named that rule out all applicable strategies for the statement,DUPSWEEDOUT is used.

If one subquery is nested within another and both are merged into a semijoin of an outer query, any specification of semijoin strategies for the innermost query are ignored.SEMIJOIN andNO_SEMIJOIN hints can still be used to enable or disable semijoin transformations for such nested subqueries.

IfDUPSWEEDOUT is disabled, on occasion the optimizer may generate a query plan that is far from optimal. This occurs due to heuristic pruning during greedy search, which can be avoided by settingoptimizer_prune_level=0.

Examples:

SELECT /*+ NO_SEMIJOIN(@subq1 FIRSTMATCH, LOOSESCAN) */ * FROM t2  WHERE t2.a IN (SELECT /*+ QB_NAME(subq1) */ a FROM t3);SELECT /*+ SEMIJOIN(@subq1 MATERIALIZATION, DUPSWEEDOUT) */ * FROM t2  WHERE t2.a IN (SELECT /*+ QB_NAME(subq1) */ a FROM t3);

Syntax of hints that affect whether to use subquery materialization orIN-to-EXISTS transformations:

SUBQUERY([@query_block_name]strategy)

The hint name is alwaysSUBQUERY.

ForSUBQUERY hints, thesestrategy values are permitted:INTOEXISTS,MATERIALIZATION.

Examples:

SELECT id, a IN (SELECT /*+ SUBQUERY(MATERIALIZATION) */ a FROM t1) FROM t2;SELECT * FROM t2 WHERE t2.a IN (SELECT /*+ SUBQUERY(INTOEXISTS) */ a FROM t1);

For semijoin andSUBQUERY hints, a leading@query_block_name specifies the query block to which the hint applies. If the hint includes no leading@query_block_name, the hint applies to the query block in which it occurs. To assign a name to a query block, seeOptimizer Hints for Naming Query Blocks.

If a hint comment contains multiple subquery hints, the first is used. If there are other following hints of that type, they produce a warning. Following hints of other types are silently ignored.

Statement Execution Time Optimizer Hints

TheMAX_EXECUTION_TIME hint is permitted only forSELECT statements. It places a limitN (a timeout value in milliseconds) on how long a statement is permitted to execute before the server terminates it:

MAX_EXECUTION_TIME(N)

Example with a timeout of 1 second (1000 milliseconds):

SELECT /*+ MAX_EXECUTION_TIME(1000) */ * FROM t1 INNER JOIN t2 WHERE ...

TheMAX_EXECUTION_TIME(N) hint sets a statement execution timeout ofN milliseconds. If this option is absent orN is 0, the statement timeout established by themax_execution_time system variable applies.

TheMAX_EXECUTION_TIME hint is applicable as follows:

  • For statements with multipleSELECT keywords, such as unions or statements with subqueries,MAX_EXECUTION_TIME applies to the entire statement and must appear after the firstSELECT.

  • It applies to read-onlySELECT statements. Statements that are not read only are those that invoke a stored function that modifies data as a side effect.

  • It does not apply toSELECT statements in stored programs and is ignored.

Variable-Setting Hint Syntax

TheSET_VAR hint sets the session value of a system variable temporarily (for the duration of a single statement). Examples:

SELECT /*+ SET_VAR(sort_buffer_size = 16M) */ name FROM people ORDER BY name;INSERT /*+ SET_VAR(foreign_key_checks=OFF) */ INTO t2 VALUES(2);SELECT /*+ SET_VAR(optimizer_switch = 'mrr_cost_based=off') */ 1;

Syntax of theSET_VAR hint:

SET_VAR(var_name =value)

var_name names a system variable that has a session value (although not all such variables can be named, as explained later).value is the value to assign to the variable; the value must be a scalar.

SET_VAR makes a temporary variable change, as demonstrated by these statements:

mysql> SELECT @@unique_checks;+-----------------+| @@unique_checks |+-----------------+|               1 |+-----------------+mysql> SELECT /*+ SET_VAR(unique_checks=OFF) */ @@unique_checks;+-----------------+| @@unique_checks |+-----------------+|               0 |+-----------------+mysql> SELECT @@unique_checks;+-----------------+| @@unique_checks |+-----------------+|               1 |+-----------------+

WithSET_VAR, there is no need to save and restore the variable value. This enables you to replace multiple statements by a single statement. Consider this sequence of statements:

SET @saved_val = @@SESSION.var_name;SET @@SESSION.var_name =value;SELECT ...SET @@SESSION.var_name = @saved_val;

The sequence can be replaced by this single statement:

SELECT /*+ SET_VAR(var_name =value) ...

StandaloneSET statements permit any of these syntaxes for naming session variables:

SET SESSIONvar_name =value;SET @@SESSION.var_name =value;SET @@.var_name =value;

Because theSET_VAR hint applies only to session variables, session scope is implicit, andSESSION,@@SESSION., and@@ are neither needed nor permitted. Including explicit session-indicator syntax results in theSET_VAR hint being ignored with a warning.

Not all session variables are permitted for use withSET_VAR. Individual system variable descriptions indicate whether each variable is hintable; seeSection 7.1.8, “Server System Variables”. You can also check a system variable at runtime by attempting to use it withSET_VAR. If the variable is not hintable, a warning occurs:

mysql> SELECT /*+ SET_VAR(collation_server = 'utf8mb4') */ 1;+---+| 1 |+---+| 1 |+---+1 row in set, 1 warning (0.00 sec)mysql> SHOW WARNINGS\G*************************** 1. row ***************************  Level: Warning   Code: 4537Message: Variable 'collation_server' cannot be set using SET_VAR hint.

SET_VAR syntax permits setting only a single variable, but multiple hints can be given to set multiple variables:

SELECT /*+ SET_VAR(optimizer_switch = 'mrr_cost_based=off')           SET_VAR(max_heap_table_size = 1G) */ 1;

If several hints with the same variable name appear in the same statement, the first one is applied and the others are ignored with a warning:

SELECT /*+ SET_VAR(max_heap_table_size = 1G)           SET_VAR(max_heap_table_size = 3G) */ 1;

In this case, the second hint is ignored with a warning that it is conflicting.

ASET_VAR hint is ignored with a warning if no system variable has the specified name or the variable value is incorrect:

SELECT /*+ SET_VAR(max_size = 1G) */ 1;SELECT /*+ SET_VAR(optimizer_switch = 'mrr_cost_based=yes') */ 1;

For the first statement, there is nomax_size variable. For the second statement,mrr_cost_based takes values ofon oroff, so attempting to set it toyes is incorrect. In each case, the hint is ignored with a warning.

TheSET_VAR hint is permitted only at the statement level. If used in a subquery, the hint is ignored with a warning.

Replicas ignoreSET_VAR hints in replicated statements to avoid the potential for security issues.

Resource Group Hint Syntax

TheRESOURCE_GROUP optimizer hint is used for resource group management (seeSection 7.1.16, “Resource Groups”). This hint assigns the thread that executes a statement to the named resource group temporarily (for the duration of the statement). It requires theRESOURCE_GROUP_ADMIN orRESOURCE_GROUP_USER privilege.

Examples:

SELECT /*+ RESOURCE_GROUP(USR_default) */ name FROM people ORDER BY name;INSERT /*+ RESOURCE_GROUP(Batch) */ INTO t2 VALUES(2);

Syntax of theRESOURCE_GROUP hint:

RESOURCE_GROUP(group_name)

group_name indicates the resource group to which the thread should be assigned for the duration of statement execution. If the group is nonexistent, a warning occurs and the hint is ignored.

TheRESOURCE_GROUP hint must appear after the initial statement keyword (SELECT,INSERT,REPLACE,UPDATE, orDELETE).

An alternative toRESOURCE_GROUP is theSET RESOURCE GROUP statement, which nontemporarily assigns threads to a resource group. SeeSection 15.7.2.4, “SET RESOURCE GROUP Statement”.

Optimizer Hints for Naming Query Blocks

Table-level, index-level, and subquery optimizer hints permit specific query blocks to be named as part of their argument syntax. To create these names, use theQB_NAME hint, which assigns a name to the query block in which it occurs:

QB_NAME(name)

QB_NAME hints can be used to make explicit in a clear way which query blocks other hints apply to. They also permit all non-query block name hints to be specified within a single hint comment for easier understanding of complex statements. Consider the following statement:

SELECT ...  FROM (SELECT ...  FROM (SELECT ... FROM ...)) ...

QB_NAME hints assign names to query blocks in the statement:

SELECT /*+ QB_NAME(qb1) */ ...  FROM (SELECT /*+ QB_NAME(qb2) */ ...  FROM (SELECT /*+ QB_NAME(qb3) */ ... FROM ...)) ...

Then other hints can use those names to refer to the appropriate query blocks:

SELECT /*+ QB_NAME(qb1) MRR(@qb1 t1) BKA(@qb2) NO_MRR(@qb3t1 idx1, id2) */ ...  FROM (SELECT /*+ QB_NAME(qb2) */ ...  FROM (SELECT /*+ QB_NAME(qb3) */ ... FROM ...)) ...

The resulting effect is as follows:

Query block names are identifiers and follow the usual rules about what names are valid and how to quote them (seeSection 11.2, “Schema Object Names”). For example, a query block name that contains spaces must be quoted, which can be done using backticks:

SELECT /*+ BKA(@`my hint name`) */ ...  FROM (SELECT /*+ QB_NAME(`my hint name`) */ ...) ...

If theANSI_QUOTES SQL mode is enabled, it is also possible to quote query block names within double quotation marks:

SELECT /*+ BKA(@"my hint name") */ ...  FROM (SELECT /*+ QB_NAME("my hint name") */ ...) ...