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

CSS XTHML writing standards and common problems summary (page optimization)

Project Documentation Directory Div+CSS Naming Sta...

Detailed explanation of Linux one-line command to process batch files

Preface The best method may not be the one you ca...

How to install suPHP for PHP5 on CentOS 7 (Peng Ge)

By default, PHP on CentOS 7 runs as apache or nob...

Vue implements 3 ways to switch tabs and switch to maintain data status

3 ways to implement tab switching in Vue 1. v-sho...

Teach you step by step to develop a brick-breaking game with vue3

Preface I wrote a few examples using vue3, and I ...

JavaScript custom plug-in to implement tab switching function

This article shares the specific code of JavaScri...

Let you understand the working principle of JavaScript

Table of contents Browser kernel JavaScript Engin...

Vue implements paging function

This article example shares the specific code of ...

Detailed explanation of how to use JavaScript paging component

The pagination component is a common component in...

Mobile web screen adaptation (rem)

Preface I recently sorted out my previous notes o...

Detailed explanation of how to cleanly uninstall Docker

First, the server environment information: Reason...

Summary of several common ways to abbreviate javascript code

Table of contents Preface Arrow Functions Master ...