Free space in MySQL after deleting tables & columns?

后端 未结 2 666
礼貌的吻别
礼貌的吻别 2020-12-19 15:59

I have a database of around 20GB. I need to delete 5 tables & drop a few columns in some other 3 tables.

Dropping 5 tables with free some 3 GB and dropping colum

相关标签:
2条回答
  • 2020-12-19 16:48

    EDIT: Doesn't apply without file_per_table, Mark is right there.

    What's going on is that once MySQL takes space, it won't give it back. This is so that if you delete 500 rows and then immediately insert 500, it doesn't have to give that space back to the file system and then request it back. It's an optimization to avoid filesystem overhead, and it works well when you delete little bits.

    If you delete a large amount, it will take a long time to end up using all that space again, which can be annoying. This can be fixed two ways: dropping the table and reloading the contents, or optimizing the table (which I believe basically reloads the table internally).

    All you have to do to get space back from a table is:

    OPTIMIZE TABLE my_big_table;
    

    Note that this can take a while, it's not a near instant operation. Basically, plan for a some downtime. If your tables are just a few gigs, it shouldn't be too long (probably a few minutes). This also rebuilds the indexes and does some other housekeeping.

    You can see more about optimize on the MySQL site. Here is it's advice:

    OPTIMIZE TABLE should be used if you have deleted a large part of a table or if you have made many changes to a table with variable-length rows (tables that have VARCHAR, VARBINARY, BLOB, or TEXT columns). Deleted rows are maintained in a linked list and subsequent INSERT operations reuse old row positions. You can use OPTIMIZE TABLE to reclaim the unused space and to defragment the data file.

    0 讨论(0)
  • 2020-12-19 16:54

    From the comments, it sounds like you're using InnoDB without the file per table option.

    Reclaiming space from the innodb tablespace is not generally possible in this mode. Your only course of action is to dump the whole database, turn on file-per-table mode, and reload it (with a completely clean mysql instance). This is going to take a long time with a large database; mk-parallel-dump and restore tools might be a bit quicker, but it will still take a while. Be sure to test this process on a non-production server first.

    0 讨论(0)
提交回复
热议问题