質問

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)

役に立ちましたか?

解決

There is no auto cleanup of fragmentation on deleting table rows from an InnoDB table..

You have to defragment the table manually.

METHOD #1

You can run the following

OPTIMIZE TABLE table1;

which will execute the following fotr you

ALTER TABLE table1 ENGINE=InnoDB;
ANALYZE TABLE table1;

This might lock the table if you still have a lot of rows (this requires downtime). Once done, the table will be smaller. Make sure you have enough diskspace for the table rebuild.

METHOD #2

If you cannot afford the downtime, you will have to use pt-online-schema-change to defrag the table. I just mentioned in a recent post : Can I disable binlogs to save space temporarily

他のヒント

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.

ライセンス: CC-BY-SA帰属
所属していません dba.stackexchange
scroll top