Why the disk space is not released after deleting data in MySQL

Why the disk space is not released after deleting data in MySQL

Problem Description

After deleting data using the delete statement in MySQL, the available disk space in the monitoring view did not increase, the disk usage did not decrease, and so on.

Solution

The reason why delete does not release space is due to MySQL's own mechanism. You need to rebuild the table to release disk space. You can refer to the following operations:

  1. Execute optimize table ${table_name}.
  2. If it is an InnoDB table, execute alter table ${table_name} engine = innodb.

There are two issues to note:

Both commands will rebuild the table. Try not to perform the operation when the disk space is tight (>90%). Expand the disk first and then shrink it after the operation.
Both commands will attempt to acquire metadata lock at the beginning and end, so try not to execute them during business peak hours.

Problem Analysis

In the MySQL mechanism, the rows deleted by delete are only marked as deleted. If there are many rows deleted and all the rows of the entire data page (innodb_page) will be deleted, the data page will only be marked as deleted. It will not be physically deleted, but will be occupied all the time, waiting to be reused.

For example:

It can be seen that data_length does not change before and after delete, but data_free increases a lot. This means that although the data has been deleted, it has not been released and is still occupied by the test1 table, but is displayed as being in a free state. When new data is written in the future, it can be reused directly without applying for new disk space.

At this time, use alter table test1 engine = innodb to see the effect:

You can see that data_length and data_free have both become empty tables, with only one innodb_page (default 16k).

PS: data_free itself can also be used to evaluate the space fragmentation of a table. When this number is very high, you can consider using the same method to rebuild the table and reclaim some disk space.

The above are the details of why the disk space is not released after MySQL Delete deletes data. For more information about why the disk space is not released after MySQL deletes data, please pay attention to other related articles on 123WORDPRESS.COM!

You may also be interested in:
  • MySQL uses frm files and ibd files to restore table data
  • MySQL export of entire or single table data
  • Solve the problem that the MySQL database crashes unexpectedly, causing the table data file to be damaged and unable to start
  • Solution to mysql failure to start due to insufficient disk space in ubuntu
  • Common problems with the MySQL storage engine MyISAM (table corruption, inaccessibility, insufficient disk space)
  • How to turn off MySQL log to protect disk space under lnmp
  • Several suggestions for shrinking MySQL to save disk space
  • How to free up disk space after deleting data in Mysql InnoDB
  • Why is the disk space still occupied after deleting table data in MySQL?

<<:  ElementUI implements cascading selector

>>:  Grid systems in web design

Recommend

Implementation of Linux command line wildcards and escape characters

If we want to perform batch operations on a type ...

Multiple methods to modify MySQL root password (recommended)

Method 1: Use the SET PASSWORD command MySQL -u r...

vue3+ts+EsLint+Prettier standard code implementation

Table of contents use Use of EsLint Add a profile...

Summary of common docker commands

Docker installation 1. Requirements: Linux kernel...

Implementation of IP address configuration in Centos7.5

1. Before configuring the IP address, first use i...

CentOS server security configuration strategy

Recently, the server has been frequently cracked ...

js to achieve a simple carousel effect

This article shares the specific code of js to ac...

JavaScript DOMContentLoaded event case study

DOMContentLoaded Event Literally, it fires after ...

Detailed steps to build a file server in Windows Server 2012

The file server is one of the most commonly used ...

How to use .htaccess to prohibit a certain IP from accessing the website

Preface For cost considerations, most webmasters ...

How to view the storage location of MySQL data files

We may have a question: After we install MySQL lo...

Example code comparing different syntax formats of vue3

The default template method is similar to vue2, u...