When data deleted from mysql, what happens to spac

2019-08-25 08:52发布

问题:

My understanding is (correct me if I'm wrong or inaccurate please) that when you delete data (say, a whole row) from a MySQL table, the file size of the database file does not shrink. If you had a 20Gb database and deleted 19Gb of data from it, you wouldn't have a 1Gb database file, it would still be 20Gb.

What happens in the "space" within the file where the data used to be? Does it get replaced by empty data? Or does the deleted data remain in the file but is "flagged" as having been deleted and can't be seen any more by regular means like running a select query? If I add more data to the database, will it go in the space where old deleted data used to be, or will it always take up new space?

For instance, in the example above, if I delete 19Gb from a 20Gb database, then add 5Gb of new data, will the database be 20Gb or 25Gb?

回答1:

It becomes free space to be reused whenever an insert is done. It can be released to the operating system via the OPTIMIZE TABLE command.