Search This Blog

Sunday, October 20, 2013

How to reclaim space in InnoDB when innodb_file_per_table is ON

How to reclaim space in InnoDB when innodb_file_per_table is ON:

When innodb_file_per_table is OFF and all data is going to be stored in ibdata files. If you drop some tables of delete some data then there is no any other way to reclaim that unused disk space except dump/reload method.
When Innodb_file_per_table is ON, each table stores data and indexes in it’s own tablespace file. However, the sharedtablespace-ibdata1 can still grow and you can check more information here about why it grows and what are the solutions.
Following the recent blog post from Miguel Angel Nieto titled “Why is the ibdata1 file continuously growing in MySQL?“, and since this is a very common question for Percona Support, this post covers how to reclaim the space when we are usinginnodb_file_per_table. Also, I will show you how to do it without causing performance or availability problems with the help of our Percona Toolkit.
When you remove rows, they are just marked as deleted on disk but space will be consumed by InnoDB files which can be re-used later when you insert/update more rows but it will never shrink. Very old MySQL bug : http://bugs.mysql.com/bug.php?id=1341
But, if you are using innodb_file_per_table then you can reclaim the space by running OPTIMIZE TABLE on that table.OPTIMIZE TABLE will create a new identical empty table. Then it will copy row by row data from old table to the new one. In this process a new .ibd tablespace will be created and the space will be reclaimed
You can see that after deleting 2M records, the test.ibd size was 168M.
After OPTIMIZE, you will be able to reclaim the space. As you can see, test.ibd file size is decreased from 168M to 68M.
I would like to mention here that during that process the table will be locked.(Table locked for just Writes) Which can affect to the performance when you’ll have large table. So If you don’t want to lock the table then you can use one of the best utility by Percona, pt-online-schema-change. It can ALTER without locking tables. You can run ALTER TABLE with ENGINE=INNODBwhich will re-create the table and reclaim the space.
(It’s always recommended to use latest version of pt-* utilities)
Same here, you can notice that test.ibd file size decreased from 157M to 56M.

No comments:

Post a Comment