17.8.10.2 Configuring Non-Persistent Optimizer Statistics Parameters (original) (raw)

17.8.10.2 Configuring Non-Persistent Optimizer Statistics Parameters

This section describes how to configure non-persistent optimizer statistics. Optimizer statistics are not persisted to disk wheninnodb_stats_persistent=OFF or when individual tables are created or altered withSTATS_PERSISTENT=0. Instead, statistics are stored in memory, and are lost when the server is shut down. Statistics are also updated periodically by certain operations and under certain conditions.

Optimizer statistics are persisted to disk by default, enabled by the innodb_stats_persistent configuration option. For information about persistent optimizer statistics, see Section 17.8.10.1, “Configuring Persistent Optimizer Statistics Parameters”.

Optimizer Statistics Updates

Non-persistent optimizer statistics are updated when:

SET GLOBAL innodb_stats_on_metadata=ON  
Configuring the Number of Sampled Pages

The MySQL query optimizer uses estimatedstatistics about key distributions to choose the indexes for an execution plan, based on the relativeselectivity of the index. When InnoDB updates optimizer statistics, it samples random pages from each index on a table to estimate thecardinality of the index. (This technique is known asrandom dives.)

To give you control over the quality of the statistics estimate (and thus better information for the query optimizer), you can change the number of sampled pages using the parameterinnodb_stats_transient_sample_pages. The default number of sampled pages is 8, which could be insufficient to produce an accurate estimate, leading to poor index choices by the query optimizer. This technique is especially important for large tables and tables used injoins. Unnecessaryfull table scans for such tables can be a substantial performance issue. SeeSection 10.2.1.23, “Avoiding Full Table Scans” for tips on tuning such queries.innodb_stats_transient_sample_pages is a global parameter that can be set at runtime.

The value ofinnodb_stats_transient_sample_pages affects the index sampling for all InnoDB tables and indexes wheninnodb_stats_persistent=0. Be aware of the following potentially significant impacts when you change the index sample size:

Whatever value ofinnodb_stats_transient_sample_pages works best for a system, set the option and leave it at that value. Choose a value that results in reasonably accurate estimates for all tables in your database without requiring excessive I/O. Because the statistics are automatically recalculated at various times other than on execution ofANALYZE TABLE, it does not make sense to increase the index sample size, runANALYZE TABLE, then decrease sample size again.

Smaller tables generally require fewer index samples than larger tables. If your database has many large tables, consider using a higher value forinnodb_stats_transient_sample_pages than if you have mostly smaller tables.