HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

MySQL: DELETE didn't shrink DB size, nor table size

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
didndeletesizeshrinkmysqlnortable

Problem

In mydatabase, there is a table table1 with almost 8M row, after deleting around 7M rows from the table, the size of the DB and the table itself remains the same.

Table size:

+---------------------------------------------+-----------+
| Table                                       | Size (MB) |
+---------------------------------------------+-----------+
| Table1                                      |     96000 |


Delete query:

DELETE FROM table1 WHERE foo < 'boo'; 
Query OK, 7809000 rows affected (43.25 sec)


Size after deleting the data:

+---------------------------------------------+-----------+
| Table                                       | Size (MB) |
+---------------------------------------------+-----------+
| Table1                                      |     96000 |


Any idea why deleting the data from this table didn't decrease the database size?

Server version: 5.7.19-17-log Percona Server (GPL)

Solution

There are several ways to delete 'most' of a table. Perhaps the best is

SET @@innodb_file_per_table = ON; -- if this is not already on  (optional)

CREATE TABLE new LIKE real;
INSERT INTO new
    SELECT * FROM real
        WHERE ... -- the rows you want to _keep_;
RENAME TABLE real TO old,
             new TO real;
DROP TABLE old;


More techiques: http://mysql.rjweb.org/doc.php/deletebig

If this will be a recurring task, see the partitioning suggestion in that link.

Code Snippets

SET @@innodb_file_per_table = ON; -- if this is not already on  (optional)

CREATE TABLE new LIKE real;
INSERT INTO new
    SELECT * FROM real
        WHERE ... -- the rows you want to _keep_;
RENAME TABLE real TO old,
             new TO real;
DROP TABLE old;

Context

StackExchange Database Administrators Q#284536, answer score: 7

Revisions (0)

No revisions yet.