Home  >  Article  >  Database  >  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?

WBOY
WBOYforward
2022-07-01 12:20:417335browse

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

    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 |
    。。。。
  2. View the specified table fragmentation situation

     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)

    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

    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

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.

    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、删除临时表

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),它重建表以更新索引统计信息并释放聚簇索引中未使用的空间。

      当您在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
    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:
This article is reproduced at:csdn.net. If there is any infringement, please contact admin@php.cn delete