Query Diagnostics in MySQL 8: Monitoring Expensive Queries by Latency and Source for Performance Optimization

Decoding MySQL Performance: Monitoring Expensive Queries by Latency and Source


Navigating through the complexity of database performance can be a challenging task, particularly when dealing with intricate queries and vast volumes of data. One effective way of managing MySQL performance is by monitoring expensive queries in terms of their latency and source. By keeping a close eye on these aspects, we can gain actionable insights into our database’s functioning, leading to improved efficiency and optimal performance. In this post, we will dive into an SQL script specifically designed for MySQL 8, aimed at overseeing these critical query parameters.

This script fetches the historical long query data from the performance_schema.events_statements_history_long table. This table contains the latest events, where an event is a statement executed by the server.

The query fetches the event ID, event name, source of the event, the latency of the event in seconds, and the SQL text of the event. It orders the result by the latency in descending order, so you get the most time-consuming queries at the top.

This script can help in troubleshooting MySQL performance in several ways:

  1. Identifying Slow Queries: The script can help you identify which queries are taking the longest time to execute. Once you’ve identified these queries, you can analyze and optimize them for better performance.
  2. Understanding Query Sources: By knowing where the slow queries are coming from, you can better understand if specific modules or parts of your application need optimization.
  3. Improving Application Performance: This script can aid in identifying which parts of your application might be experiencing slowdowns due to slow queries, thereby helping you enhance the overall application performance.

Remember, while this script is useful for identifying expensive queries, it is only one piece of the puzzle when it comes to optimizing MySQL performance. You should also consider other factors like hardware resources, database design, and query optimization techniques when looking to improve your MySQL performance.


In summary, the proposed SQL script delivers a practical, hands-on approach to monitor MySQL’s performance. By pinpointing the most time-consuming queries and identifying their sources, we can focus our optimization efforts where they’re needed most. However, while this script is a powerful tool for identifying potential bottlenecks, remember that it’s just one piece of the performance optimization puzzle. Factors such as hardware resources, database design, and overall query optimization techniques should also be taken into account for a holistic performance improvement strategy. As we continue to delve deeper into the world of MySQL performance, scripts like these will be invaluable for enhancing our understanding and improving our database operations.

About Shiv Iyer 456 Articles
Open Source Database Systems Engineer with a deep understanding of Optimizer Internals, Performance Engineering, Scalability and Data SRE. Shiv currently is the Founder, Investor, Board Member and CEO of multiple Database Systems Infrastructure Operations companies in the Transaction Processing Computing and ColumnStores ecosystem. He is also a frequent speaker in open source software conferences globally.