MySQL's Performance Schema is a built-in database engine that monitors MySQL's internal runtime performance information. The enable method is as follows: 1. The default majority of versions are enabled, and you can check the status by SHOW VARIABLES LIKE 'performance_schema'; 2. If OFF, add performance_schema=ON in the [mysqld] part of my.cnf or my.ini, and restart it will take effect; 3. Note that the old version may need to be compiled manually. Common monitoring tables include: 1. events_statements_summary_by_digest is used to locate the most resource-consuming SQL; 2. table_io_waits_summary_by_table is used to locate the hotspot table; 3. file_summary_by_instance is used to monitor disk IO; 4. memory_summary_global_by_event_name is used to view memory usage. Performance impact: 1. Low overhead, with the general CPU and memory increasing by less than 5%; 2. Enable too many instruments may increase the load; 3. Overhead can be reduced by turning off non-essential acquisition items. How to use the tool: 1. Use sys schema to simplify query; 2. Prometheus Grafana to achieve visual monitoring; 3. Use pt-query-digest to analyze SQL performance problems. With these core content mastered, Performance Schema can become an efficient problem diagnosis tool.
MySQL's Performance Schema is a built-in database engine that monitors performance information in MySQL's internal runtime. It does not only focus on SQL statements like slow query logs, but instead captures detailed indicators such as threads, locks, file IO, memory usage, etc. from a lower level. If you find that the database is slowing down but can’t find the obvious reason, Performance Schema is your “magnifying glass”.

How to enable Performance Schema?
By default, Performance Schema is enabled for most MySQL versions, but you have to check if it is actually on. You can view it through the following command:

SHOW VARIABLES LIKE 'performance_schema';
If ON
is returned, it means it is enabled; if it is OFF
, you need to add it to the configuration file (usually my.cnf
or my.ini
):
[mysqld] performance_schema=ON
Restart MySQL and check again.

Note: Some older versions (such as before MySQL 5.6) may not be turned on by default, or relevant parameters need to be added during manual compilation.
What are the commonly used monitoring items? How should I look at it?
Performance Schema organizes data into multiple tables, which you can understand as "observation points". Here are a few practical and easy to use watches:
events_statements_summary_by_digest
This table can help you quickly find the most resource-consuming SQL statements. It summarizes the number of executions, average time, lock waiting time and other information according to the SQL template (digest). Suitable for troubleshooting "what SQL is the most frequent or slowest".table_io_waits_summary_by_table
If you suspect that a table access is too slow, you can see which table has the longest waiting time for reading and writing in this table. Suitable for positioning hot spot table issues.file_summary_by_instance
Monitor the disk IO performance and you can see which data files (ibdata1, ib_logfile, etc.) have the greatest read and write pressure.memory_summary_global_by_event_name
Check the amount of memory used by different modules, such as InnoDB buffer pools, temporary tables, and connection-related memory allocation.
The data of these tables will not be cleaned automatically, so it is recommended to back up or export key fields regularly for analysis.
Does the performance impact have a big impact? Do you want to worry about the overhead?
This is a question that many people are concerned about. While Performance Schema is a lightweight design, it is logging internal events after all, so there is definitely an extra overhead. However, in general, its impact is very small, with the CPU and memory increasing by less than 5% in most scenarios.
Of course, if you turn on a large number of instruments (that is, acquisition items), especially high-frequency acquisition points such as "statement/sql/%" and "wait/io/file/%", it will still have some impact on performance.
One tip: You can reduce overhead by closing unwanted collection items based on actual needs. For example:
UPDATE performance_schema.setup_instruments SET ENABLED = 'NO' WHERE NAME LIKE 'stage/%';This will disable some unimportant phase events and reduce resource usage.
How to use it in combination with tools?
Although it is useful to check the Performance Schema table separately, it is not intuitive enough. You can use the following methods to improve efficiency:
- Use
sys
schema: It is an official view encapsulation based on Performance Schema, simplifying many complex queries.- Combined with Prometheus Grafana: Crawl the key metrics of Performance Schema regularly and make a visual panel.
- In conjunction with pt-query-digest: Although it is mainly used to analyze slow query logs, it can also be viewed in combination with events_statements_summary data.
These tools can not only let you see historical trends, but also help set alarm rules to achieve early warning.
Basically that's it. Performance Schema is not particularly difficult to understand, but there are many details and we need to look at it in combination with specific issues. As long as you master a few core tables and basic ideas, it is a very practical diagnostic tool.
The above is the detailed content of Using and interpreting MySQL Performance Schema. For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

GTID (Global Transaction Identifier) ??solves the complexity of replication and failover in MySQL databases by assigning a unique identity to each transaction. 1. It simplifies replication management, automatically handles log files and locations, allowing slave servers to request transactions based on the last executed GTID. 2. Ensure consistency across servers, ensure that each transaction is applied only once on each server, and avoid data inconsistency. 3. Improve troubleshooting efficiency. GTID includes server UUID and serial number, which is convenient for tracking transaction flow and accurately locate problems. These three core advantages make MySQL replication more robust and easy to manage, significantly improving system reliability and data integrity.

MySQL main library failover mainly includes four steps. 1. Fault detection: Regularly check the main library process, connection status and simple query to determine whether it is downtime, set up a retry mechanism to avoid misjudgment, and can use tools such as MHA, Orchestrator or Keepalived to assist in detection; 2. Select the new main library: select the most suitable slave library to replace it according to the data synchronization progress (Seconds_Behind_Master), binlog data integrity, network delay and load conditions, and perform data compensation or manual intervention if necessary; 3. Switch topology: Point other slave libraries to the new master library, execute RESETMASTER or enable GTID, update the VIP, DNS or proxy configuration to

The steps to connect to the MySQL database are as follows: 1. Use the basic command format mysql-u username-p-h host address to connect, enter the username and password to log in; 2. If you need to directly enter the specified database, you can add the database name after the command, such as mysql-uroot-pmyproject; 3. If the port is not the default 3306, you need to add the -P parameter to specify the port number, such as mysql-uroot-p-h192.168.1.100-P3307; In addition, if you encounter a password error, you can re-enter it. If the connection fails, check the network, firewall or permission settings. If the client is missing, you can install mysql-client on Linux through the package manager. Master these commands

IndexesinMySQLimprovequeryspeedbyenablingfasterdataretrieval.1.Theyreducedatascanned,allowingMySQLtoquicklylocaterelevantrowsinWHEREorORDERBYclauses,especiallyimportantforlargeorfrequentlyqueriedtables.2.Theyspeedupjoinsandsorting,makingJOINoperation

InnoDB is MySQL's default storage engine because it outperforms other engines such as MyISAM in terms of reliability, concurrency performance and crash recovery. 1. It supports transaction processing, follows ACID principles, ensures data integrity, and is suitable for key data scenarios such as financial records or user accounts; 2. It adopts row-level locks instead of table-level locks to improve performance and throughput in high concurrent write environments; 3. It has a crash recovery mechanism and automatic repair function, and supports foreign key constraints to ensure data consistency and reference integrity, and prevent isolated records and data inconsistencies.

MySQL's default transaction isolation level is RepeatableRead, which prevents dirty reads and non-repeatable reads through MVCC and gap locks, and avoids phantom reading in most cases; other major levels include read uncommitted (ReadUncommitted), allowing dirty reads but the fastest performance, 1. Read Committed (ReadCommitted) ensures that the submitted data is read but may encounter non-repeatable reads and phantom readings, 2. RepeatableRead default level ensures that multiple reads within the transaction are consistent, 3. Serialization (Serializable) the highest level, prevents other transactions from modifying data through locks, ensuring data integrity but sacrificing performance;

MySQL transactions follow ACID characteristics to ensure the reliability and consistency of database transactions. First, atomicity ensures that transactions are executed as an indivisible whole, either all succeed or all fail to roll back. For example, withdrawals and deposits must be completed or not occur at the same time in the transfer operation; second, consistency ensures that transactions transition the database from one valid state to another, and maintains the correct data logic through mechanisms such as constraints and triggers; third, isolation controls the visibility of multiple transactions when concurrent execution, prevents dirty reading, non-repeatable reading and fantasy reading. MySQL supports ReadUncommitted and ReadCommi.

To add MySQL's bin directory to the system PATH, it needs to be configured according to the different operating systems. 1. Windows system: Find the bin folder in the MySQL installation directory (the default path is usually C:\ProgramFiles\MySQL\MySQLServerX.X\bin), right-click "This Computer" → "Properties" → "Advanced System Settings" → "Environment Variables", select Path in "System Variables" and edit it, add the MySQLbin path, save it and restart the command prompt and enter mysql--version verification; 2.macOS and Linux systems: Bash users edit ~/.bashrc or ~/.bash_
