Monitor Azure Database for MySQL performance with Query Store
Applies to: Azure Database for MySQL 5.7, 8.0
The Query Store feature in Azure Database for MySQL provides a way to track query performance over time. Query Store simplifies performance troubleshooting by helping you quickly find the longest running and most resource-intensive queries. Query Store automatically captures a history of queries and runtime statistics, and it retains them for your review. It separates data by time windows so that you can see database usage patterns. Data for all users, databases, and queries is stored in the mysql schema database in the Azure Database for MySQL instance.
Common scenarios for using Query Store
Query store can be used in a number of scenarios, including the following:
- Detecting regressed queries
- Determining the number of times a query was executed in a given time window
- Comparing the average execution time of a query across time windows to see large deltas
Enabling Query Store
Query Store is an opt-in feature, so it isn't active by default on a server. The query store is enabled or disabled globally for all the databases on a given server and cannot be turned on or off per database.
Enable Query Store using the Azure portal
- Sign in to the Azure portal and select your Azure Database for MySQL server.
- Select Server Parameters in the Settings section of the menu.
- Search for the query_store_capture_mode parameter.
- Set the value to ALL and Save.
To enable wait statistics in your Query Store:
- Search for the query_store_wait_sampling_capture_mode parameter.
- Set the value to ALL and Save.
Allow up to 20 minutes for the first batch of data to persist in the mysql database.
Information in Query Store
Query Store has two stores:
- A runtime statistics store for persisting the query execution statistics information.
- A wait statistics store for persisting wait statistics information.
To minimize space usage, the runtime execution statistics in the runtime statistics store are aggregated over a fixed, configurable time window. The information in these stores is visible by querying the query store views.
The following query returns information about queries in Query Store:
SELECT * FROM mysql.query_store;
Or this query for wait statistics:
SELECT * FROM mysql.query_store_wait_stats;
Finding wait queries
Wait statistics should not be enabled during peak workload hours or be turned on indefinitely for sensitive workloads.
For workloads running with high CPU utilization or on servers configured with lower vCores, use caution when enabling wait statistics. It should not be turned on indefinitely.
Wait event types combine different wait events into buckets by similarity. Query Store provides the wait event type, specific wait event name, and the query in question. Being able to correlate this wait information with the query runtime statistics means you can gain a deeper understanding of what contributes to query performance characteristics.
Here are some examples of how you can gain more insights into your workload using the wait statistics in Query Store:
|High Lock waits||Check the query texts for the affected queries and identify the target entities. Look in Query Store for other queries modifying the same entity, which is executed frequently and/or have high duration. After identifying these queries, consider changing the application logic to improve concurrency, or use a less restrictive isolation level.|
|High Buffer IO waits||Find the queries with a high number of physical reads in Query Store. If they match the queries with high IO waits, consider introducing an index on the underlying entity, to do seeks instead of scans. This would minimize the IO overhead of the queries. Check the Performance Recommendations for your server in the portal to see if there are index recommendations for this server that would optimize the queries.|
|High Memory waits||Find the top memory consuming queries in Query Store. These queries are probably delaying further progress of the affected queries. Check the Performance Recommendations for your server in the portal to see if there are index recommendations that would optimize these queries.|
When Query Store is enabled it saves data in 15-minute aggregation windows, up to 500 distinct queries per window.
The following options are available for configuring Query Store parameters.
|query_store_capture_mode||Turn the query store feature ON/OFF based on the value. Note: If performance_schema is OFF, turning on query_store_capture_mode will turn on performance_schema and a subset of performance schema instruments required for this feature.||ALL||NONE, ALL|
|query_store_capture_interval||The query store capture interval in minutes. Allows specifying the interval in which the query metrics are aggregated||15||5 - 60|
|query_store_capture_utility_queries||Turning ON or OFF to capture all the utility queries that is executing in the system.||NO||YES, NO|
|query_store_retention_period_in_days||Time window in days to retain the data in the query store.||7||1 - 30|
The following options apply specifically to wait statistics.
|query_store_wait_sampling_capture_mode||Allows turning ON / OFF the wait statistics.||NONE||NONE, ALL|
|query_store_wait_sampling_frequency||Alters frequency of wait-sampling in seconds. 5 to 300 seconds.||30||5-300|
Currently query_store_capture_mode supersedes this configuration, meaning both query_store_capture_mode and query_store_wait_sampling_capture_mode have to be enabled to ALL for wait statistics to work. If query_store_capture_mode is turned off, then wait statistics is turned off as well since wait statistics utilizes the performance_schema enabled, and the query_text captured by query store.
Views and functions
View and manage Query Store using the following views and functions. Anyone in the select privilege public role can use these views to see the data in Query Store. These views are only available in the mysql database.
Queries are normalized by looking at their structure after removing literals and constants. If two queries are identical except for literal values, they will have the same hash.
This view returns all the data in Query Store. There is one row for each distinct database ID, user ID, and query ID.
||varchar(64)||NO||Name of the schema|
||bigint(20)||NO||Unique ID generated for the specific query, if the same query executes in different schema, a new ID will be generated|
||timestamp||NO||Timestamp in which the query is executed. This is based on the query_store_interval configuration|
||longtext||NO||The normalized query text after removing all the literals|
||longtext||NO||First appearance of the actual query with literals|
||bit||YES||Whether the query text has been truncated. Value will be Yes if the query is longer than 1 KB|
||bigint(20)||NO||The number of times the query got executed for this timestamp ID / during the configured interval period|
||bigint(20)||NO||Number of warnings this query generated during the internal|
||bigint(20)||NO||Number of errors this query generated during the interval|
||double||YES||Total execution time of this query during the interval|
||double||YES||Average execution time for this query during the interval|
||double||YES||Minimum execution time for this query|
||double||YES||Maximum execution time|
||bigint(20)||NO||Total amount of time spent for all the locks for this query execution during this time window|
||bigint(20)||NO||Number of rows affected|
||bigint(20)||NO||Number of rows sent to client|
||bigint(20)||NO||Number of rows examined|
||bigint(20)||NO||Number of full joins|
||bigint(20)||NO||Number of select scans|
||bigint(20)||NO||Number of rows sorted|
||bigint(20)||NO||Number of times when the query did not use any indexes|
||bigint(20)||NO||Number of times when the query execution engine did not use any good indexes|
||bigint(20)||NO||Total number of temp tables created|
||bigint(20)||NO||Total number of temp tables created in disk (generates I/O)|
||timestamp||NO||The first occurrence (UTC) of the query during the aggregation window|
||timestamp||NO||The last occurrence (UTC) of the query during this aggregation window|
This view returns wait events data in Query Store. There is one row for each distinct database ID, user ID, query ID, and event.
||timestamp||NO||Start of the interval (15-minute increment)|
||timestamp||NO||End of the interval (15-minute increment)|
||bigint(20)||NO||Generated unique ID on the normalized query (from query store)|
||varchar(32)||NO||The normalized query text after removing all the literals (from query store)|
||longtext||NO||First appearance of the actual query with literals (from query store)|
||varchar(32)||NO||Category of the wait event|
||varchar(128)||NO||Name of the wait event|
||bigint(20)||NO||Number of wait events sampled during the interval for the query|
||double||NO||Total wait time (in milliseconds) of this query during the interval|
||Purges all query store data before the given time stamp|
||Purges all wait event data before the given time stamp|
||Purges recommendations whose expiration is before the given time stamp|
Limitations and known issues
- If a MySQL server has the parameter
default_transaction_read_onlyon, Query Store cannot capture data.
- Query Store functionality can be interrupted if it encounters long Unicode queries (>= 6000 bytes).
- The retention period for wait statistics is 24 hours.
- Wait statistics uses sample to capture a fraction of events. The frequency can be modified using the parameter
- Learn more about Query Performance Insights