Documentation Home
MySQL 5.7 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 35.1Mb
PDF (A4) - 35.2Mb
Man Pages (TGZ) - 256.4Kb
Man Pages (Zip) - 361.2Kb
Info (Gzip) - 3.4Mb
Info (Zip) - 3.4Mb
Excerpts from this Manual

MySQL 5.7 Reference Manual  / ...  / Optimization  / Controlling the Query Optimizer  /  Switchable Optimizations

8.9.2 Switchable Optimizations

Theoptimizer_switch system variable enables control over optimizer behavior. Its value is a set of flags, each of which has a value ofon oroff to indicate whether the corresponding optimizer behavior is enabled or disabled. This variable has global and session values and can be changed at runtime. The global default can be set at server startup.

To see the current set of optimizer flags, select the variable value:

mysql> SELECT @@optimizer_switch\G*************************** 1. row ***************************@@optimizer_switch: index_merge=on,index_merge_union=on,                    index_merge_sort_union=on,                    index_merge_intersection=on,                    engine_condition_pushdown=on,                    index_condition_pushdown=on,                    mrr=on,mrr_cost_based=on,                    block_nested_loop=on,batched_key_access=off,                    materialization=on,semijoin=on,loosescan=on,                    firstmatch=on,duplicateweedout=on,                    subquery_materialization_cost_based=on,                    use_index_extensions=on,                    condition_fanout_filter=on,derived_merge=on,                    prefer_ordering_index=on

To change the value ofoptimizer_switch, assign a value consisting of a comma-separated list of one or more commands:

SET [GLOBAL|SESSION] optimizer_switch='command[,command]...';

Eachcommand value should have one of the forms shown in the following table.

Command SyntaxMeaning
defaultReset every optimization to its default value
opt_name=defaultSet the named optimization to its default value
opt_name=offDisable the named optimization
opt_name=onEnable the named optimization

The order of the commands in the value does not matter, although thedefault command is executed first if present. Setting anopt_name flag todefault sets it to whichever ofon oroff is its default value. Specifying any givenopt_name more than once in the value is not permitted and causes an error. Any errors in the value cause the assignment to fail with an error, leaving the value ofoptimizer_switch unchanged.

The following list describes the permissibleopt_name flag names, grouped by optimization strategy:

When you assign a value tooptimizer_switch, flags that are not mentioned keep their current values. This makes it possible to enable or disable specific optimizer behaviors in a single statement without affecting other behaviors. The statement does not depend on what other optimizer flags exist and what their values are. Suppose that all Index Merge optimizations are enabled:

mysql> SELECT @@optimizer_switch\G*************************** 1. row ***************************@@optimizer_switch: index_merge=on,index_merge_union=on,                    index_merge_sort_union=on,                    index_merge_intersection=on,                    engine_condition_pushdown=on,                    index_condition_pushdown=on,                    mrr=on,mrr_cost_based=on,                    block_nested_loop=on,batched_key_access=off,                    materialization=on,semijoin=on,loosescan=on,                    firstmatch=on,duplicateweedout=on,                    subquery_materialization_cost_based=on,                    use_index_extensions=on,                    condition_fanout_filter=on,derived_merge=on,                    prefer_ordering_index=on

If the server is using the Index Merge Union or Index Merge Sort-Union access methods for certain queries and you want to check whether the optimizer performs better without them, set the variable value like this:

mysql> SET optimizer_switch='index_merge_union=off,index_merge_sort_union=off';mysql> SELECT @@optimizer_switch\G*************************** 1. row ***************************@@optimizer_switch: index_merge=on,index_merge_union=off,                    index_merge_sort_union=off,                    index_merge_intersection=on,                    engine_condition_pushdown=on,                    index_condition_pushdown=on,                    mrr=on,mrr_cost_based=on,                    block_nested_loop=on,batched_key_access=off,                    materialization=on,semijoin=on,loosescan=on,                    firstmatch=on,duplicateweedout=on,                    subquery_materialization_cost_based=on,                    use_index_extensions=on,                    condition_fanout_filter=on,derived_merge=on,                    prefer_ordering_index=on