Performance Schema is MySQL’s built-in instrumentation system — every statement, every wait, every lock can be measured with sub-microsecond accuracy. Left on the defaults, it is also the largest source of overhead on a busy MySQL server. This article is the operator-grade configuration we apply: enabling the consumers that actually inform decisions, muting the ones that cost more than they reveal, and pointing the schema at the queries that justify the cost.
How to verify
Confirm Performance Schema is enabled and inventory what is being consumed. The setup_* tables describe what is collected; the events_* tables hold the data.
mysql -e "SHOW VARIABLES LIKE 'performance_schema';"
mysql -e "SELECT NAME, ENABLED, TIMED FROM performance_schema.setup_instruments WHERE ENABLED='YES' LIMIT 20;"
mysql -e "SELECT * FROM performance_schema.setup_consumers;"
mysql -e "SELECT * FROM sys.metrics WHERE Variable_name LIKE '%performance_schema%' LIMIT 20;"
mysql -e "SHOW ENGINE PERFORMANCE_SCHEMA STATUS\G" | head -40
If performance_schema=OFF, the server is not collecting anything and you have to restart to enable it — Performance Schema is a startup-only variable. The events_statements_summary_by_digest table is the high-value target; if it is empty, the statement consumers are off.
What’s happening
Performance Schema’s data model has three layers. Instruments are the things that can be measured — every mutex, every file, every statement type, every wait event has an instrument name like statement/sql/select or wait/io/file/innodb/innodb_data_file. Consumers are downstream aggregations that store the data — events_statements_current holds the in-flight statement per thread, events_statements_summary_by_digest aggregates by query fingerprint. Threads decide whose activity is observed.
When an instrument is enabled, MySQL pays a small cost on every event of that type — typically tens of nanoseconds for a counter increment plus the cost of writing into a ring buffer. Enable thousands of instruments on a million-events-per-second server and the cost becomes measurable. Enable them with timing (TIMED='YES'), and every event also reads the high-resolution clock; on some hardware that clock read is the bottleneck.
The sys schema is a set of views and procedures on top of Performance Schema that frame the raw data into operator-useful queries. sys.statements_with_runtimes_in_95th_percentile answers “which queries are in the slow tail?” sys.schema_table_lock_waits shows what is blocking on what. We almost never query performance_schema.* directly — we go through sys.*.
The procedure
-
Confirm Performance Schema is on. If it is off, enable it in
my.cnfand restart. There is no way to turn it on without a restart.[mysqld] performance_schema = ON -
Decide which consumers to enable. The defaults in MySQL 8 are sensible for most workloads. For a high-throughput OLTP server, we keep statements summary on, drop the per-thread events, and disable the most expensive wait instruments.
UPDATE performance_schema.setup_consumers SET ENABLED='YES' WHERE NAME IN ('events_statements_current', 'events_statements_history_long', 'global_instrumentation', 'statements_digest'); UPDATE performance_schema.setup_consumers SET ENABLED='NO' WHERE NAME IN ('events_waits_current', 'events_waits_history', 'events_waits_history_long'); -
Disable specific instrument families that produce noise without action.
UPDATE performance_schema.setup_instruments SET ENABLED='NO', TIMED='NO' WHERE NAME LIKE 'wait/synch/cond/%' OR NAME LIKE 'wait/lock/metadata/%'; -
Persist the consumer changes across restarts. The
setup_*tables are in-memory only — write them intoperformance-schema-consumer-*variables inmy.cnf.[mysqld] performance_schema = ON performance-schema-consumer-events-waits-current = OFF performance-schema-consumer-events-waits-history = OFF performance-schema-consumer-events-waits-history-long = OFF performance-schema-consumer-events-statements-history-long = ON performance-schema-consumer-statements-digest = ON -
Find the queries that account for most of the time. The
sysschema view does the math.SELECT db, exec_count, total_latency, avg_latency, rows_examined_avg, query FROM sys.statement_analysis ORDER BY total_latency DESC LIMIT 20; -
Watch how much memory Performance Schema itself is consuming. On a busy box this can hit several GB.
SELECT event_name, current_alloc, high_alloc FROM sys.memory_global_by_current_bytes WHERE event_name LIKE 'memory/performance_schema/%' ORDER BY current_alloc DESC LIMIT 20; SHOW ENGINE PERFORMANCE_SCHEMA STATUS\G -
If you need lower overhead, prune the in-memory tables. The
performance_schema_*_sizevariables cap each consumer’s footprint.[mysqld] performance_schema_events_statements_history_long_size = 1000 performance_schema_digests_size = 5000 performance_schema_max_table_instances = 5000
Common pitfalls
- Leaving every wait instrument on — on a high-concurrency InnoDB server,
wait/synch/mutex/innodb/*events fire millions of times per second. Disable the families you do not actually examine. - Trying to turn Performance Schema on without a restart — the
performance_schemavariable is startup-only. Changes to consumers and instruments are runtime; changes to the framework are not. - Using
events_statements_currentfor SHOW PROCESSLIST-style queries — that consumer holds one row per thread; it is cheap. Usingevents_statements_historyis also cheap; usingevents_statements_history_longis fine for periodic harvest, not for tight polling. - Storing aggregated digest data in the server forever — the digest table caps at
performance_schema_digests_size. Once full, low-traffic queries get evicted. Snapshot to an external store if you want long retention. - Sleeping on
sys.x$*views — thex$variants return raw numbers (picoseconds, bytes), not the human-friendly strings. Usesys.x$*when you are feeding the output into another query or a tool.
Performance Schema turns “MySQL feels slow” into “this digest is 60% of total latency and the explain plan shows a missing index.” In the engagements we run, the Performance Schema configuration is part of the host’s MySQL build — consumers and instrument families tuned for the workload, the sys schema queries used by our on-call playbook, and digest summaries fed into the observability stack alongside pt-query-digest snapshots. For the broader pattern of running databases with this level of instrumentation, see /en/services/managed-operations/.