Adaptive execution plan switching

Updated at:

In some scenarios, the optimizer has difficulty accurately estimating the number of table rows scanned, predicate selectivity, and execution plan costs. PolarDB for MySQL provides adaptive execution plan switching, which automatically adjusts execution plans based on actual data. This topic describes the background information and usage of adaptive execution plan switching.

Background information

The optimizer estimates cardinality and selectivity based on statistics and some sampled data. This information may lead to large estimation deviations due to sampling accuracy, sampling methods, and other reasons. In addition, there are many scenarios in which no relevant statistics are available. In such cases, the database estimates statistics based on empirical values or some mathematical assumption, which may cause a large deviation between the estimated value and the actual value. When the optimizer's estimate does not match the actual situation and a wrong execution plan is selected, it is difficult for the optimizer to collect the corresponding information in advance.

Adaptive execution plan switching

Note

Adaptive execution plan switching currently supports two modes: adaptive row-column routing and adaptive selection of ordered indexes.

Adaptive row-column routing

When you use the In-Memory Column Index (IMCI) feature and have configured automatic row-column routing, a query may be routed to row store instead of column store for execution because the estimated cost of row store execution is too low.

The previous solutions to the routing error described above are as follows:

  • Lower the cost threshold for routing to column store. However, this solution can easily route a large number of short queries to column store, overloading column store and blocking other queries.

  • Use the Statement Outline feature to route queries that use a specified template to column store. For example, adding the outline in the following example forces the query statement to be executed on column store. However, this approach greatly increases O&M costs.

    CALL dbms_outln.add_optimizer_outline("<Schema_name>", "/*+ SET_VAR(cost_threshold_for_imci=0) SET_VAR(imci_ap_threshold=0) */","<query>");

The adaptive execution plan switching of PolarDB for MySQL can be applied to row-column routing to automatically switch slow queries that are incorrectly routed to row store to column store, ensuring execution efficiency. As shown in the following figure, with adaptive execution, the database marks the number of scanned rows of each query block and of the entire query during the optimization phase and adds this information to the monitoring data. If the optimizer does not select column store after calculating the cost, it calculates the threshold for triggering adaptive execution. For clusters of new versions (such as 8.0.1.1.49 or later or 8.0.2.2.29 or later), the threshold is calculated based on the loose_adaptive_cost_threshold_for_imci parameter. For clusters of earlier versions, the threshold is calculated based on the loose_cost_threshold_for_imci parameter. During the execution phase, when the number of scanned rows of a query block or of the entire query reaches the threshold for triggering adaptive execution, the system checks whether to switch to column store execution. Monitoring the number of scanned rows on the core path is only an integer comparison and does not affect execution performance. Before switching to column store, the system also ensures that the result set of the row store execution has not been returned to the client. After switching to column store, the cached row store result set is cleared.

image

Adaptive selection of ordered indexes

In database query optimization, prefer_ordering_index is an optimization option that instructs the query optimizer to preferentially use indexes that support sorting. In some cases, however, this optimization may degrade query performance. In particular, when the selectivity of the ordered index does not match the selectivity of the query, choosing the ordered index may cause the query to scan a large amount of irrelevant data due to the low selectivity of the ordered index or extensive table lookups, increasing the I/O and CPU load.

The solutions to the slow queries caused by prefer_ordering_index described above are as follows:

You can use the Statement Outline feature to disable prefer_ordering_index for queries that use a specified SQL template. However, this approach greatly increases O&M costs. In addition, if some queries that use this SQL template are more efficient with prefer_ordering_index enabled, they are also affected.

CALL dbms_outln.add_optimizer_outline("<Schema_name>", "/*+ SET_VAR(optimizer_switch='prefer_ordering_index=off') */","<query>");

Adaptive execution can be applied to correct the selection of ordered indexes. When a query performs poorly because of the prefer_ordering_index optimization, the execution plan is adjusted again so that the plan is corrected away from the ordered index. If a query selects an ordered index because of the prefer_ordering_index optimization, the optimizer calculates the threshold for triggering adaptive execution. During the execution phase, when the number of rows executed on the ordered index meets the threshold condition, the system checks whether to switch the execution plan and re-execute. Monitoring the number of scanned rows on the core path is only an integer comparison and does not affect execution performance. Before switching the execution plan, the system also ensures that the execution result set has not been returned to the client. After switching the plan, the cached row store result set is cleared.

Prerequisites

  • The cluster requirements for enabling adaptive row-column routing (the imci_chosen switch in Usage) are as follows:

    • PolarDB for MySQL 8.0.1, revision version 8.0.1.1.39 or later.

    • PolarDB for MySQL 8.0.2, revision version 8.0.2.2.29 or later.

  • The cluster requirements for enabling adaptive selection of ordered indexes (the ordering_index switch in Usage) are as follows:

    • PolarDB for MySQL 8.0.1, revision version 8.0.1.1.47 or later.

    • PolarDB for MySQL 8.0.2, revision version 8.0.2.2.29 or later.

Usage

Enable or disable adaptive execution plan switching

Log in to the PolarDB console。 On the Parameters page of the target cluster, configure adaptive execution plan switching:

  • Set the loose_adaptive_plans_switch parameter to 'imci_chosen=on' to enable adaptive row-column routing.

  • Set the loose_adaptive_plans_switch parameter to 'ordering_index=on' to enable adaptive adjustment of the ordered indexes selected by the prefer_ordering_index optimization.

  • Set the loose_adaptive_plans_switch parameter to 'imci_chosen=on,ordering_index=on' to enable both adaptive capabilities at the same time, or set the value of either one to 'off' to disable the corresponding adaptive capability.

For detailed instructions on setting parameter values, see Configure cluster and node parameters. The specific parameters involved in using adaptive execution plan switching are described in the following table.

Parameter

Level

Description

loose_adaptive_plans_switch

Global/Session

Switch that controls adaptive execution plan switching. Valid values:

  • imci_chosen: Switch that controls whether adaptive execution is used in row-column routing.

    • ON (default): Enables adaptive execution in row-column routing. Column store nodes must still be correctly configured for it to take effect.

    • OFF: Disables adaptive execution in row-column routing.

  • ordering_index: Switch that controls adaptive selection of ordered indexes in row-column routing.

    • ON: Enables adaptive adjustment of ordered index selection.

    • OFF (default): Disables adaptive adjustment of ordered index selection.

loose_adaptive_cost_threshold_for_imci

Global/Session

Sets the cost threshold for adaptive row-column routing.

Valid values: 1 to 18446744073709551615. Default value: 50000.

Note

This parameter applies to the following database engine versions:

  • PolarDB for MySQL 8.0.1, revision version 8.0.1.1.49 or later.

  • PolarDB for MySQL 8.0.2, revision version 8.0.2.2.29 or later.

loose_adaptive_plans_max_time

Global/Session

The maximum elapsed execution time of a SQL statement for which adaptive execution plan switching is allowed. If a query statement has been executed under the original plan for longer than this time, the execution plan is no longer switched even if the switching threshold is triggered.

Valid values: 0 to 1800000. Default value: 500. Unit: milliseconds.

loose_adaptive_ordering_rows_threshold

Global/Session

Sets the checkpoint for adaptive selection of ordered indexes. A smaller valid index value causes the adaptive check and adjustment to be performed earlier.

Valid values: 0 to 4294967295. Default value: 50000.

View the number of adaptive plan switches

You can execute the following SQL statement in the database to view the number of adaptive plan switches from the time adaptive execution plan switching was enabled up to now.

SHOW GLOBAL STATUS LIKE 'Adaptive_plan_used';

The Adaptive_plan_used variable is described as follows:

Variable

Level

Description

Adaptive_plan_used

Global

The number of adaptive plan switches from the time adaptive execution plan switching was enabled up to now.