This article uses an example to illustrate the page splitting of MySQL clustered indexes. Share with you for your reference, the details are as follows: In MySQL, MyISAM uses non-clustered indexes, and the InnoDB storage engine uses clustered indexes. Characteristics of cluster structure:
Why does page split occur? This is because the clustered index uses a balanced binary tree algorithm, and each node stores the data of the row corresponding to the primary key. Assuming that the primary key of the inserted data is self-increasing, the binary tree algorithm will quickly add the data to a certain node, while other nodes do not need to be moved; but if irregular data is inserted, each insertion will change the previous data status of the binary tree. This results in page splits. test: Create 2 tables create table t8( id int primary key, c1 varchar(500), c2 varchar(500), c3 varchar(500), c4 varchar(500), c5 varchar(500), c6 varchar(500) ) engine innodb charset utf8; create table t9( id int primary key, c1 varchar(500), c2 varchar(500), c3 varchar(500), c4 varchar(500), c5 varchar(500), c6 varchar(500) ) engine innodb charset utf8; Write a PHP script to insert 10,000 irregular primary key data and 10,000 regular primary key data to see the difference. <?php set_time_limit(0); $conn = mysql_connect('localhost','root','1234'); mysql_query('use test;'); //Self-increment primary key $str = str_repeat('a', 500); $startTime = microtime(true); for($i=1;$i<=10000;$i++){ mysql_query("insert into t8 values($i,'$str','$str','$str','$str','$str','$str')"); } $endTime = microtime(true); echo $endTime-$startTime.'<br/>'; //Unordered primary key $arr = range(1, 10000); shuffle($arr); $startTime = microtime(true); foreach($arr as $i){ mysql_query("insert into t9 values($i,'$str','$str','$str','$str','$str','$str')"); } $endTime = microtime(true); echo $endTime-$startTime.'<br/>'; Test result graph Data for 10,000 rules: 998 seconds = 16 minutes in conclusion: The primary key value of the clustered index should be a continuously increasing value instead of a random value (do not use a random string or UUID), otherwise it will cause a large number of page splits and page moves. When using InnoDB, it is best defined as: Readers who are interested in more MySQL-related content can check out the following topics on this site: "Summary of MySQL Index Operation Skills", "Summary of MySQL Common Functions", "Summary of MySQL Log Operation Skills", "Summary of MySQL Transaction Operation Skills", "Summary of MySQL Stored Procedure Skills" and "Summary of MySQL Database Lock-Related Skills". I hope this article will be helpful to everyone's MySQL database design. You may also be interested in:
|
<<: Modify the jvm encoding problem when Tomcat is running
>>: Vue's global watermark implementation example
CEP - Complex Event Processing. The payment has n...
Library Management Create a library create databa...
Description: Set a timer to replace the content of...
Due to the default bridge network, the IP address...
Many web pages have small triangles in their navi...
This article shares the specific code for JavaScr...
1. Edit the PAM configuration file sudo vim /etc/...
Since this is my first post, if there are any mis...
In this article, I will show you how to install a...
When we make a gradient background color, we will...
First, check whether the hard disk device has a d...
Table of contents 1. Demand Background 2. Optimiz...
Because the project needs to use https service, I...
The property of centering text in CSS is very simp...
Table of contents 1.sleep function 2. setTimeout ...