MySQL Performance Schema: Query Profiling and Wait Event Analysis

MySQL Performance Schema: query profiling and wait analysis for WordPress

The Performance Schema is MySQL’s built-in instrumentation: it records statement times, lock waits, I/O waits and memory use per query pattern, with far less overhead than the general query log. It is enabled by default since MySQL 5.6, and the sys schema (MySQL 5.7+) gives readable views on top of it. When the slow query log and EXPLAIN do not explain a slowdown, the Performance Schema shows where the time actually goes: in executing, or in waiting for locks and disk.

Problem: A WordPress database has intermittent slow queries, but the slow log and EXPLAIN do not reveal the cause: the time is lost in waits (locks, I/O), not in the query plan.

Solution: Use the statement digest summary to find the query patterns with the most total time, the table I/O and lock wait summaries to find the hot tables, and the history tables to break one statement down into stages.

The examples use the database name helloadmin; replace it with the name of your WordPress database (or with DATABASE() to use the current one).

Is it on?

SHOW VARIABLES LIKE 'performance_schema';   -- ON

If it is OFF, it can only be enabled in the server configuration (performance_schema = ON) and a restart.

Top query patterns by total time

SELECT DIGEST_TEXT AS query_pattern,
       COUNT_STAR AS executions,
       ROUND( SUM_TIMER_WAIT / 1e12, 2 ) AS total_sec,
       ROUND( AVG_TIMER_WAIT / 1e9, 2 ) AS avg_ms,
       SUM_ROWS_EXAMINED AS rows_examined,
       SUM_NO_INDEX_USED AS no_index_used
FROM performance_schema.events_statements_summary_by_digest
WHERE SCHEMA_NAME = 'helloadmin'
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 20;

Queries are grouped by their normalized form (values replaced with ?), so a query that is fast but runs 50,000 times shows up next to one that is slow but rare. The timers are in picoseconds, hence the divisions. The same in a readable form: SELECT * FROM sys.statement_analysis LIMIT 20;.

Hot tables: I/O and locks

-- Time spent reading and writing each table
SELECT OBJECT_NAME AS tbl,
       COUNT_READ AS reads, COUNT_WRITE AS writes,
       ROUND( SUM_TIMER_READ / 1e12, 2 ) AS read_sec,
       ROUND( SUM_TIMER_WRITE / 1e12, 2 ) AS write_sec
FROM performance_schema.table_io_waits_summary_by_table
WHERE OBJECT_SCHEMA = 'helloadmin'
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 10;

-- Time spent waiting for table locks
SELECT OBJECT_NAME AS tbl,
       COUNT_STAR AS lock_waits,
       ROUND( SUM_TIMER_WAIT / 1e12, 2 ) AS lock_wait_sec
FROM performance_schema.table_lock_waits_summary_by_table
WHERE OBJECT_SCHEMA = 'helloadmin'
ORDER BY SUM_TIMER_WAIT DESC
LIMIT 10;

On WordPress the usual suspects are wp_options (autoloaded options read on every request, and transients written to it), wp_postmeta and the tables of plugins that log every visit. Row lock waits inside InnoDB are visible in sys.innodb_lock_waits while they happen.

One statement, stage by stage

For a detailed profile, enable the history consumers for the time of the investigation:

UPDATE performance_schema.setup_consumers
SET ENABLED = 'YES'
WHERE NAME IN ( 'events_statements_history_long', 'events_stages_history_long' );

UPDATE performance_schema.setup_instruments
SET ENABLED = 'YES', TIMED = 'YES'
WHERE NAME LIKE 'stage/%';

Run the slow query, then find it and its stages:

SELECT EVENT_ID, LEFT( SQL_TEXT, 80 ) AS query, ROUND( TIMER_WAIT / 1e9, 2 ) AS ms
FROM performance_schema.events_statements_history_long
ORDER BY EVENT_ID DESC
LIMIT 10;

-- Replace 12345 with the EVENT_ID of the query
SELECT EVENT_NAME, ROUND( TIMER_WAIT / 1e9, 2 ) AS ms
FROM performance_schema.events_stages_history_long
WHERE NESTING_EVENT_ID = 12345
ORDER BY EVENT_ID;

A long stage/sql/Sending data or executing means reading rows (an index problem), a long waiting for handler commit points to slow disk writes, and lock stages to contention.

These changes last until the server restarts. Switch them off again when you are done, they add overhead:

UPDATE performance_schema.setup_consumers
SET ENABLED = 'NO'
WHERE NAME IN ( 'events_statements_history_long', 'events_stages_history_long' );

Reset between measurements

TRUNCATE TABLE performance_schema.events_statements_summary_by_digest;
TRUNCATE TABLE performance_schema.table_io_waits_summary_by_table;

Then load the page or run the test, and read the summaries again: they show only what happened since the reset.

NOTE: The default instrumentation (statement digests and table waits) is cheap and can stay on in production. The history and stage consumers are the expensive part: enable them for an investigation, not permanently. Reading the Performance Schema needs the SELECT privilege on it, and changing the setup tables needs UPDATE; on shared hosting you may have neither, then the slow query log is the tool.