Table of Contents
Summary of several defragmentation solutions for MySQL (to solve the problem of space not being released after deleting a large amount of data) Questions)
1. Background knowledge?
1.1 Why are there fragments?
1.2 Problems caused by fragmentation
2. How to clean up debris?
2.1. View table fragmentation
2.2 Methods to clean up fragments (reclaim space)
通常有这几种做法
1. alter table tb_test engine=innodb 原理介绍
2. optimize table xxx;
3. alter table、analyze table和optimize table区别
4. Which is better, OPTIMIZE TABLE or ALTER TABLE xxxx ENGINE= INNODB?
2.3 Official recommendations
Home Database Mysql Tutorial How does MySQL solve the problem of space not being released after deleting a large amount of data?

How does MySQL solve the problem of space not being released after deleting a large amount of data?

Jul 01, 2022 pm 12:20 PM
mysql

This article brings you relevant knowledge about mysql, which mainly sorts out the related problems of not releasing space after deleting a large amount of data. Both insert and update in MySQL may cause page splits. There are fragments in this way. Let's take a look at them together. I hope it will be helpful to everyone.

How does MySQL solve the problem of space not being released after deleting a large amount of data?

Recommended study: mysql video tutorial

Summary of several defragmentation solutions for MySQL (to solve the problem of space not being released after deleting a large amount of data) Questions)

1. Background knowledge?

1.1 Why are there fragments?

  1. Both insert and update in MySQL May cause the page to be split, so fragmentation exists.

    For a large number of UPDATEs, file fragmentation will also occur. The minimum physical storage allocation unit of Innodb is a page, and UPDATEs may also cause page splits. Frequent page splits will cause the page to be fragmented. Become sparse and filled irregularly, so eventually the data will be fragmented.

  2. The delete statement actually just marks the data and records it in a linked list, thus forming a blank space.

    In InnoDB, when some rows are deleted, these rows are only marked as "deleted" instead of being physically deleted from the index, so the space is not really released and reclaimed. InnoDB's Purge thread will asynchronously clean up these useless index keys and rows.

  3. When performing an insert operation, MySQL will try to use blank space, but if a certain blank space has not been occupied by data of appropriate size, it still cannot be completely occupied, resulting in Fragmentation;

  4. Summary:

    1. #The addition, deletion and modification operations of the table may cause data holes. When a large number of addition, deletion and modification operations are performed on the table Finally, data holes are more likely to exist.

    2. MySQL deletes data in several situations and whether to release disk space:

      1. drop, truncate releases disk space immediately, whether it is Innodb And MyISAM;
      • truncate table is actually a bit similar to drop table and then create, except that the process of creating table has been optimized, for example, the table structure file has been existing before, etc. Therefore, the speed should be close to the speed of drop table;
      1. delete from table_name deletes all the data in the table. For MyISAM, the disk space will be released immediately (it should be specially processed, which is more reasonable) ), InnoDB will not release disk space;
      2. For delete from table_name where xxx; conditional deletion, neither innodb nor MyISAM will release disk space;
      3. Use optimize after delete operation table table_name will free up disk space immediately. Whether it is innodb or myisam. Therefore, to achieve the purpose of freeing up disk space, perform the optimize table operation after delete.
      4. Although the disk space is not released after deleting the from table, this space can still be used the next time data is inserted.

1.2 Problems caused by fragmentation

  • When MySQL scans the data, it scans The object is actually the upper limit of the capacity requirement of the list, which is the peak position of the area where the data is written;

  • The table in the MySQL database undergoes multiple deletes, updates, and After insert, the table space will become fragmented. Regularly defragmenting the table space and eliminating fragmentation can improve the performance of accessing the table space.

  • This kind of fragmentation not only increases the storage cost, but also reduces the scanning efficiency of the table because of data fragmentation.

  • If the fragments are not defragmented, they may occupy disk space for a long time, resulting in higher and higher disk usage.

2. How to clean up debris?

The prerequisite for fixing the problem is to find the problem first, so that we can prescribe the right remedy.

2.1. View table fragmentation

  1. View every fragmented table in the database

    1

    2

    3

    4

    5

    6

    7

    8

    9

    mysql> select concat('optimize table ',table_schema,'.',table_name,';'),data_free,engine from information_schema.tables where data_free>0 and engine !='MEMORY';

    +-----------------------------------------------------------+-----------+--------+

    | concat('optimize table ',table_schema,'.',table_name,';') | DATA_FREE | ENGINE |

    +-----------------------------------------------------------+-----------+--------+

    | optimize table abc.t_user_answer;                          |   2097152 | InnoDB |

    | optimize table mysql.time_zone_transition;                |   4194304 | InnoDB |

    | optimize table mysql.time_zone_transition_type;           |   4194304 | InnoDB |

    | optimize table mysql.user;                                |   4194304 | InnoDB |

    。。。。

    Copy after login
  2. View the specified table fragmentation situation

    1

    2

    3

    4

    5

    6

    7

    8

    9

    10

    11

    12

    13

    14

    15

    16

    17

    18

    19

    20

    21

     mysql> show table status like 't_user'\G

     *************************** 1. row ***************************

                Name: t_user

              Engine: InnoDB

             Version: 10

          Row_format: Dynamic

                Rows: 4333

      Avg_row_length: 589

         Data_length: 2555904

     Max_data_length: 0

        Index_length: 2719744

           Data_free: 4194304

      Auto_increment: NULL

         Create_time: 2021-11-19 10:13:31

         Update_time: 2022-04-20 14:28:42

          Check_time: NULL

           Collation: utf8mb4_general_ci

            Checksum: NULL

      Create_options:

             Comment:

     1 row in set (0.00 sec)

    Copy after login

    Data_free: 4194304 represents the number of bytes in the fragment. If the data table is frequently deleted, a large number of Data_free records will be frequently deleted or tables with variable length fields will be modified.

  3. Find the most severely fragmented table

    1

    2

    3

    4

    5

    SELECT table_schema, TABLE_NAME, concat(data_free/1024/1024, 'M'as data_free

    FROM `information_schema`.tables

    WHERE data_free > 3 * 1024 * 1024

        AND ENGINE = 'innodb'

    ORDER BY data_free DESC

    Copy after login

2.2 Methods to clean up fragments (reclaim space)

Official Document Reference
How does MySQL solve the problem of space not being released after deleting a large amount of data?

通常有这几种做法

  1. alter table tb_test engine=innodb; (本质上是 recreate)
  2. optimize table tb_test; (本质上是 recreate,但是在不同创建下会有区别)
  3. ALTER TABLE tablename FORCE (在InnoDB表中等价于 alter table tb_test engine=innodb; )
  4. mysqlcheck 批量表空间优化
  5. gh-ost/pt-osc
  6. pt-online-schema-change (本质上也是 先备份旧表数据,然后 truncate 旧表)

1. alter table tb_test engine=innodb 原理介绍

这其实是一个NULL操作,表面上看什么也不做,实际上重新整理碎片了.当执行优化操作时,实际执行的是一个空的 ALTER 命令,但是这个命令也会起到优化的作用,它会重建整个表,删掉未使用的空白空间.

Running ALTER TABLE tbl_name ENGINE=INNODB on an existing InnoDB table performs a “null” ALTER TABLE operation, which can be used to defragment an InnoDB table, as described in Section 15.11.4, “Defragmenting a Table”. Running ALTER TABLE tbl_name FORCE on an InnoDB table performs the same function.

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

    MySQL5.6 开始采用 Inplace 方式重建表,Alter 期间,支持 DML 查询和更新操作,语句为 alter table t engine=innodb, ALGORITHM=inplace;之所以支持 DML 更新操作,是因为数据拷贝期间会将 DML 更新操作记录到 Row log 中。

 

    重建过程中最耗时的就是拷贝数据的过程,这个过程中支持 DML 查询和更新操作,对于整个 DDL 来说,锁时间很短,就可以近似认为是 Online DDL。

 

    执行过程:

 

    1、获取 MDL(Meta Data Lock)写锁,innodb 内部创建与原表结构相同的临时文件

 

    2、拷贝数据之前,MDL 写锁退化成 MDL 读锁,支持 DML 更新操作

 

    3、根据主键递增顺序,将一行一行的数据读出并写入到临时文件,直至全部写入完成。并且,会将拷贝期间的 DML 更新操作记录到 Row log 中

 

    4、上锁,再将 Row log 中的数据应用到临时文件

 

    5、互换原表和临时表表名

 

    6、删除临时表

Copy after login

2. optimize table xxx;

OPTIMIZE TABLE语句可以重新组织表、索引的物理存储,减少存储空间,提高访问的I/O效率。类似于碎片整理功能。

MySQL可以通过optimize table语句释放表空间,重组表数据和索引的物理页,减少表所占空间和优化读写性能

  1. 使用语法

    OPTIMIZE [LOCAL | NO_WRITE_TO_BINLOG] TABLE tbl_n说ame [, tbl_name] …

    • 对于主从架构, LOCAL 参数可以让这个过程不写入 binlog ,这样在主库上执行时就不会同步给从库了
    • 默认情况下,MySQL将OPTIMIZE TABLE语句写入二进制日志,以便它们复制到slave服务器。如果不想写二进制日志,使用命令时加上NO_WRITE_To_BINLOG或LOCAL关键字即可。
    • 使用这个语句需要具有对目标表的SELECT、INSERT权限。
  2. 注意:

    1. 需要有足够的空间才能进行OPTIMIZE TABLE。 (剩余空间必须 > 被 OPTIMIZE 的表的大小)

    2. OPTIMIZE 只对独立表空间(innodb_file_per_table=1)才有用,对共享表空间不起作用。

      对于共享表空间,如果需要瘦身: 必须将数据导出,删除ibdata1,然后将 innodb_file_per_table 设置为独立表空间, 然后将数据导入进来。

    3. 对于InnoDB的表,OPTIMIZE TABLE 的工作原理如下

      对于InnoDB表, OPTIMIZE TABLE映射到ALTER TABLE … FORCE(或者这样翻译:在InnoDB表中等价 ALTER TABLE … FORCE),它重建表以更新索引统计信息并释放聚簇索引中未使用的空间。

      1

      2

      3

      4

      5

      6

      7

      8

      9

      10

      11

      当您在InnoDB表上运行时,它会显示在OPTIMIZE TABLE的输出中,如下所示:

      mysql> OPTIMIZE TABLE foo; 

      +----------+----------+----------+---------------------------------------+ 

      | Table    | Op       | Msg_type | Msg_text                                                          | 

      +----------+----------+----------+---------------------------------------+ 

      | test.foo | optimize | note     | Table does not support optimize, doing recreate + analyze instead | 

      | test.foo | optimize | status   | OK                                                                | 

      +----------+----------+----------+---------------------------------------+ 

       

      # 但这个提示语可以忽略,从严格的意义讲,说InnoDB不支持optimize table,其实不太准确。 因为 MYSQL的文档说明了,当INNODB 的表,MYSQL会以 ALTER TABLE force  +  analyze 去执行这个命令(相当于做了recreate和analyze)。 所以最终还是会看到 OK 的状态。 

      # https://stackoverflow.com/questions/30635603/what-does-table-does-not-support-optimize-doing-recreate-analyze-instead-me

      Copy after login
    4. 对于MYISAM表,OPTIMIZE TABLE 的工作原理:
      1. 如果表已删除或分隔行,就修复该表。
      2. 如果索引页没有排序,就排序它们。
      3. 如果表的统计信息不是最新的(而且修复不能通过对索引进行排序),就更新它们。

    5. **执行时也可以发现报错: Temporary file write failure. **

      建议参考这片文章:
      Mysql optimize table 时报错 Temporary file write failure. 的解决方案

  3. optimize 语句的官网介绍

    • 如果您已经删除了表的一大部分,或者如果您已经对含有可变长度行的表(含有VARCHAR, BLOB或TEXT列的表)进行了很多更改,则应使用 OPTIMIZE TABLE。

    • 被删除的记录被保持在链接清单中,后续的INSERT操作会重新使用旧的记录位置。您可以使用OPTIMIZE TABLE来重新利用未使用的空间,并整理数据文件的碎片。

    • 在多数的设置中,您根本不需要运行OPTIMIZE TABLE。即使您对可变长度的行进行了大量的更新,您也不需要经常运行,每周一次或每月一次 即可,只对特定的表运行。

  4. Mysql 5.6 之前 在OPTIMIZE TABLE运行过程中,MySQL会锁定表,5.6之后有了 Online DDL 则大大减少了锁表时间。

3. alter table、analyze table和optimize table区别

  • alter table tb_test engine = innodb;

    • (也就是 recreate)MySQL 5.5以前用Offline的方式重建表,5.6以后用Online的方式重建表;
  • analyze table tb_test ;

    • 重新统计表的索引信息,不会修改数据,不会重建表,整个过程加MDL读
  • optimize table tb_test ;

    • is the process of alter table xxx = innodb; analyze table xxx;.

4. Which is better, OPTIMIZE TABLE or ALTER TABLE xxxx ENGINE= INNODB?

  • OPTIMIZE TABLE or ALTER TABLE xxxx ENGINE= INNODB Basically the same. But in some cases, ALTER TABLE xxxx ENGINE= INNODB is better.
    • For example: old_alter_table system variable is not enabled, etc.
  • In addition: For MyISAM type tables, using ALTER TABLE xxxx ENGINE= INNODB is obviously better than OPTIMIZE TABLE.

2.3 Official recommendations

MySQL officially recommends not to defragment frequently (hourly or daily). Generally, depending on the actual situation, it only needs to be defragmented once a week or monthly ( We are now clearing table fragments under all mysql instances at 4 a.m. every month)

Recommended learning: mysql video tutorial

The above is the detailed content of How does MySQL solve the problem of space not being released after deleting a large amount of data?. For more information, please follow other related articles on the PHP Chinese website!

Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

The relationship between mysql user and database The relationship between mysql user and database Apr 08, 2025 pm 07:15 PM

In MySQL database, the relationship between the user and the database is defined by permissions and tables. The user has a username and password to access the database. Permissions are granted through the GRANT command, while the table is created by the CREATE TABLE command. To establish a relationship between a user and a database, you need to create a database, create a user, and then grant permissions.

MySQL: The Ease of Data Management for Beginners MySQL: The Ease of Data Management for Beginners Apr 09, 2025 am 12:07 AM

MySQL is suitable for beginners because it is simple to install, powerful and easy to manage data. 1. Simple installation and configuration, suitable for a variety of operating systems. 2. Support basic operations such as creating databases and tables, inserting, querying, updating and deleting data. 3. Provide advanced functions such as JOIN operations and subqueries. 4. Performance can be improved through indexing, query optimization and table partitioning. 5. Support backup, recovery and security measures to ensure data security and consistency.

How to fill in mysql username and password How to fill in mysql username and password Apr 08, 2025 pm 07:09 PM

To fill in the MySQL username and password: 1. Determine the username and password; 2. Connect to the database; 3. Use the username and password to execute queries and commands.

Query optimization in MySQL is essential for improving database performance, especially when dealing with large data sets Query optimization in MySQL is essential for improving database performance, especially when dealing with large data sets Apr 08, 2025 pm 07:12 PM

1. Use the correct index to speed up data retrieval by reducing the amount of data scanned select*frommployeeswherelast_name='smith'; if you look up a column of a table multiple times, create an index for that column. If you or your app needs data from multiple columns according to the criteria, create a composite index 2. Avoid select * only those required columns, if you select all unwanted columns, this will only consume more server memory and cause the server to slow down at high load or frequency times For example, your table contains columns such as created_at and updated_at and timestamps, and then avoid selecting * because they do not require inefficient query se

Can I retrieve the database password in Navicat? Can I retrieve the database password in Navicat? Apr 08, 2025 pm 09:51 PM

Navicat itself does not store the database password, and can only retrieve the encrypted password. Solution: 1. Check the password manager; 2. Check Navicat's "Remember Password" function; 3. Reset the database password; 4. Contact the database administrator.

How to view mysql How to view mysql Apr 08, 2025 pm 07:21 PM

View the MySQL database with the following command: Connect to the server: mysql -u Username -p Password Run SHOW DATABASES; Command to get all existing databases Select database: USE database name; View table: SHOW TABLES; View table structure: DESCRIBE table name; View data: SELECT * FROM table name;

How to create navicat premium How to create navicat premium Apr 09, 2025 am 07:09 AM

Create a database using Navicat Premium: Connect to the database server and enter the connection parameters. Right-click on the server and select Create Database. Enter the name of the new database and the specified character set and collation. Connect to the new database and create the table in the Object Browser. Right-click on the table and select Insert Data to insert the data.

How to copy tables in mysql How to copy tables in mysql Apr 08, 2025 pm 07:24 PM

Copying a table in MySQL requires creating new tables, inserting data, setting foreign keys, copying indexes, triggers, stored procedures, and functions. The specific steps include: creating a new table with the same structure. Insert data from the original table into a new table. Set the same foreign key constraint (if the original table has one). Create the same index. Create the same trigger (if the original table has one). Create the same stored procedure or function (if the original table is used).

See all articles