For relational databases, the design of tables and SQL is written are particularly crucial. It wouldn’t be an exaggeration to say that they account for 90% of performance. So this time, specifically targeting these two major knowledge areas, we’ll conduct a detailed analysis for you, peeling back the layers.
This Series uses plain and understandable language and selects a large number of examples to elaborate on the subtleties for you.
🧑💻 Target audience:
DBA
Database developers
Students
We will use MySQL as the demonstration database.
The merging and splitting of data pages or secondary index pages (referred to as data pages or index pages) in MySQL InnoDB tables significantly impact overall table performance; the more these operations occur, the greater the impact on data writes.
MySQL provides a data page merge threshold (MERGE_THRESHOLD), which can be manually adjusted in certain scenarios to reduce page merging and splitting.
In InnoDB tables, each data page is 16K by default. The default MERGE_THRESHOLD is 50, with a range of 1 to 50, where the default is also the maximum value. When the record occupancy of a page falls below 50%, MySQL merges the page with adjacent pages to maintain compactness and avoid waste.
1. Trigger Scenarios for the Threshold
Scenario 1: Page A originally has 100% data occupancy. After some records are deleted, occupancy drops below 50%, triggering the threshold.
Scenario 2: Page B contains records updated to a shorter form (e.g., a value changes from rpad('I love everyone', 10000, 'add redundant characters') to 'I only love you'). This reduction in record size causes page occupancy to drop below 50%, triggering the threshold.
2. Data Page Merging Explained
After deleting some records, if the remaining records in Page A occupy less than the MERGE_THRESHOLD, and an adjacent page (Page C) also has less than 50% occupancy, MySQL merges Page C’s records into Page A. Page C’s space is then freed up for future inserts, avoiding waste.
3. Data Page Splitting Explained
Consider Pages D and E, each with 49% record occupancy. After merging, Page D’s occupancy becomes 98%, leaving only 2% free space. Similarly, Pages F and H, each at 49% occupancy, merge to 98% on Page F.
When a new insert request comes in with a primary key between Pages D and F, and neither has enough space (only 2% left), Page D must split. A new page (Page I) is created. The original records from Page D and the new insert are sorted and redistributed: Page D is refilled, and the remaining data goes to Page I. Frequent page splitting involves significant overhead due to data migration and new page creation.
4. Practical Use and Monitoring of MERGE_THRESHOLD
Note: MERGE_THRESHOLD must be in uppercase! MySQL treats lowercase as a simple comment.
4.1 Setting MERGE_THRESHOLD for a Table
To set MERGE_THRESHOLD for an entire table, include the value in the table comment:
This article introduces the concept of the MySQL index page merge threshold and evaluates its impact on index page merging and splitting in practical environments. Feedback and suggestions are welcome.
SQLFlash is your AI-powered SQL Optimization Partner.
Based on AI models, we accurately identify SQL performance bottlenecks and optimize query performance, freeing you from the cumbersome SQL tuning process so you can fully focus on developing and implementing business logic.