Advanced InnoDB Performance Monitoring: Using Statistics and Forecasts for Optimization

Introduction- Advanced InnoDB Performance Monitoring

InnoDB, the storage engine for MySQL, offers a powerful platform for storing and retrieving data efficiently. However, as your database grows and workloads increase, performance issues might arise. To proactively address these issues, monitoring and analyzing InnoDB performance becomes essential. In this blog, we’ll explore how to troubleshoot InnoDB performance using advanced statistics and forecast formulas with the SQL script provided below.

-- Title: InnoDB Performance Monitoring with Advanced Statistics and Forecast Formulas

-- This script utilizes the events_statements_summary_by_digest table in the MySQL 8 performance_schema
-- to monitor InnoDB performance using advanced statistics and forecast formulas.

SELECT
    -- Selecting relevant columns for analysis
    ssbd.SCHEMA_NAME,
    LEFT(ssbd.DIGEST_TEXT, 100) AS QuerySnippet,
    ssbd.COUNT_STAR,
    ssbd.SUM_TIMER_WAIT,
    ssbd.SUM_ROWS_EXAMINED,
    ssbd.SUM_ROWS_AFFECTED,
    ssbd.SUM_SELECT_SCAN,
    -- Calculating average time per query
    (ssbd.SUM_TIMER_WAIT / ssbd.COUNT_STAR) AS Avg_Time_Per_Query,
    -- Converting total time to seconds
    (ssbd.SUM_TIMER_WAIT / 1000000000000) AS Total_Time_Seconds,
    ssbd.MAX_TOTAL_MEMORY,
    ssbd.QUANTILE_95,
    ssbd.QUANTILE_99,
    ssbd.QUANTILE_999,
    -- Joining with information_schema.TABLES to fetch table and index sizes
    IFNULL(t.DATA_LENGTH + t.INDEX_LENGTH, 0) AS Table_Size,
    IFNULL(t.INDEX_LENGTH, 0) AS Index_Size
FROM
    events_statements_summary_by_digest ssbd
    LEFT JOIN information_schema.TABLES t ON ssbd.SCHEMA_NAME = t.TABLE_SCHEMA
WHERE
    ssbd.SUM_TIMER_WAIT > 0
ORDER BY
    Total_Time_Seconds DESC
LIMIT 10;

Understanding the Script

The script utilizes the events_statements_summary_by_digest table from the MySQL 8 performance_schema to extract valuable performance-related insights. Here’s what the key parts of the script do: 

  1. Selecting Relevant Columns: First, the script selects important columns for analysis. These include schema name, a snippet of the query text, query performance metrics, and memory usage.
  2. Calculating Metrics: Next, the script calculates several metrics to provide a comprehensive view of InnoDB performance. It calculates the average time per query (Avg_Time_Per_Query) by dividing the total wait time by the number of queries (COUNT_STAR). It also converts the wait time to seconds to determine Total_Time_Seconds.
  3. Fetching Table and Index Sizes: The script enhances the analysis by joining with information_schema.TABLES. It fetches the size of the associated table and indexes to help understand how query performance might be affected by data size.
  4. Filtering and Ordering: Finally, the script filters out rows where the wait time is greater than zero. This ensures only significant queries are considered. It then orders the result set by total time in descending order, highlighting queries with the highest impact on performance.

Conclusion

Monitoring and troubleshooting InnoDB performance is essential for maintaining a healthy and efficient database. The script leverages advanced statistics and forecast formulas available in MySQL 8 performance_schema. As a result, it provides valuable insights into query efficiency, memory utilization, and potential bottlenecks. By using this script, you can explore InnoDB performance data effectively and make well-informed optimization decisions.

About Shiv Iyer 497 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.