MySQL sys Schema Performance Tuning Guide
The MySQL sys schema is a powerful, built-in toolset
designed to simplify database administration and performance tuning.
This article explores how the sys schema interprets complex
data from the performance_schema and
information_schema to provide actionable insights,
highlighting its key views, stored procedures, and practical
applications for optimizing query performance and resource
utilization.
Understanding the MySQL sys Schema
The performance_schema in MySQL monitors database events
and performance metrics in near real-time. However, its raw tables are
highly complex, containing hundreds of millions of rows of granular data
that can be difficult to interpret.
The sys schema acts as a user-friendly abstraction layer
over the performance_schema. It organizes this raw
instrumentation data into formatted, easily readable views, functions,
and stored procedures. Instead of writing complex multi-table joins to
diagnose a bottleneck, database administrators (DBAs) and developers can
query simple sys views to get immediate answers.
How the sys Schema Simplifies Performance Tuning
The sys schema simplifies the performance tuning process
in several key ways:
1. Human-Readable Formatting
By default, the performance_schema tracks time in
picoseconds and memory usage in bytes. The sys schema
automatically formats these values into human-readable units, such as
milliseconds, seconds, megabytes, and gigabytes, making instant analysis
much easier.
2. Identifying Slow and Expensive Queries
Finding resource-heavy queries is critical for database optimization.
The sys schema provides views that aggregate query
execution statistics: *
statement_analysis: Displays details about
executed queries, including which queries run the most frequently, take
the longest to execute, or return the most errors. *
statements_with_temp_tables: Pinpoints
queries that create temporary tables on disk, which can severely degrade
performance. *
statements_with_full_table_scans:
Identifies queries running without indexes, forcing MySQL to scan entire
tables.
3. Finding Unused and Redundant Indexes
While indexes speed up read operations, unused indexes waste storage
space and slow down write operations (INSERT, UPDATE, DELETE). The
sys schema helps clean up database indexing: *
schema_unused_indexes: Lists all indexes
that have not been used since the server started. *
schema_redundant_indexes: Highlights
duplicate or redundant indexes that can be safely dropped.
4. Diagnosing Memory Usage
Memory leaks and misallocated buffers can cause database crashes. The
sys schema tracks memory allocation across the entire
server: * memory_global_by_current_bytes:
Displays which internal events, threads, or buffers are currently
consuming the most memory. *
memory_by_thread_by_current_bytes:
Pinpoints specific user connections or system background threads that
are monopolizing RAM.
5. Analyzing Locks and Blocked Sessions
When one query blocks another, database latency spikes. The
sys schema makes it simple to trace locks: *
innodb_lock_waits: Shows which
transactions are currently blocked, what resources they are waiting for,
and which session is holding the blocking lock.
Key sys Schema Stored Procedures for Rapid Diagnosis
In addition to static views, the sys schema includes
several utility procedures that automate diagnostic tasks:
sys.diagnostics(): Generates a comprehensive HTML or text report of the current state of the MySQL server, capturing CPU, memory, active transactions, and query bottlenecks.sys.ps_setup_show_enabled(): Displays which performance schema instruments and consumers are currently enabled, helping you configure monitoring without impacting server performance.