Showing posts with label Performance Schema. Show all posts
Showing posts with label Performance Schema. Show all posts

Thursday, March 23, 2023

Mysql : Performance Schema


 










Performance Schema is a feature in MySQL that provides instrumentation for monitoring database performance. It allows you to collect data on MySQL server activity, such as queries, locks, and resource utilization.


To use Performance Schema, you need to enable it in your MySQL configuration file. Once enabled, you can use the Performance Schema tables to query performance data and analyze the performance of your MySQL server.


Here are the basic steps for using Performance Schema:


Enable Performance Schema in your MySQL configuration file by adding the following line:


performance_schema=ON

 

Once enabled, you can use the Performance Schema tables to query performance data. For example, to see the top 10 queries by execution time, you can run the following query:


SELECT * FROM performance_schema.events_statements_summary_by_digest ORDER BY SUM_TIMER_WAIT DESC LIMIT 10;

You can also use Performance Schema to track specific events, such as query execution or table locks, by setting up instrument-specific filters.


For example, to track all SELECT queries that take longer than 1 second to execute, you can run the following query:


UPDATE performance_schema.setup_consumers SET ENABLED='YES' WHERE NAME='events_statements_history_long';

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

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

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

UPDATE performance_schema.setup_consumers SET ENABLED='YES' WHERE NAME='events_statements_history_long';

SELECT * FROM performance_schema.events_statements_history_long WHERE COMMAND_TYPE = 'SELECT' AND TIMER_WAIT > 1000000000;


By using Performance Schema, you can gain valuable insights into the performance of your MySQL server and optimize it for better performance. 


You can also use Performance Schema to monitor resource usage, such as CPU and memory consumption, by querying the performance_schema.global_status and performance_schema.global_variables tables.


For example, to see the total amount of memory used by MySQL, you can run the following query:


SELECT VARIABLE_VALUE FROM performance_schema.global_variables WHERE VARIABLE_NAME='innodb_buffer_pool_size';



This will return the size of the InnoDB buffer pool, which is a key component of MySQL's memory usage.

You can further customize Performance Schema by configuring various options such as the frequency of data collection and the number of rows to store in each table. These options can be set in the MySQL configuration file or at runtime using the SET statement.


For example, to set the maximum number of rows to store in the performance_schema.events_statements_summary_by_digest table to 1000, you can run the following query:


SET PERFORMANCE_SCHEMA_MAX_STATEMENTS_HISTORY=1000;



In summary, Performance Schema is a powerful tool for monitoring and optimizing the performance of your MySQL server. By enabling it and using its tables and filters, you can gain insights into database activity, resource usage, and more.


It's worth noting that while Performance Schema provides valuable insights into MySQL performance, it can also have a performance impact of its own. Collecting performance data can use system resources, so it's important to use Performance Schema judiciously and to balance the trade-off between data accuracy and system performance.


In addition to using Performance Schema directly, you can also use third-party tools that integrate with it, such as MySQL Workbench or the Percona Monitoring and Management tool. These tools provide graphical interfaces for analyzing Performance Schema data and can help you identify performance bottlenecks and optimize your MySQL server.


Finally, it's important to keep Performance Schema up-to-date with your MySQL version. New releases of MySQL may introduce new instrumentation or change the behavior of existing instrumentation, so it's important to test and update Performance Schema when upgrading MySQL.


In conclusion, Performance Schema is a powerful feature in MySQL that provides instrumentation for monitoring database performance. By enabling it and using its tables and filters, you can gain insights into database activity, resource usage, and more, and optimize your MySQL server for better performance. However, it's important to use Performance Schema judiciously and to balance the trade-off between data accuracy and system performance.