How to reclaim space in InnoDB when innodb_file_per_table is ON

MySQL

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 shared tablespace-ibdata1 can still grow and you can check more information here about why it grows and what are the solutions.

https://www.percona.com/blog/2013/08/20/why-is-the-ibdata1-file-continuously-growing-in-mysql/

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 using innodb_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

Shell

mysql> select count() From test; +———-+ | count() |
+———-+
| 3145728 |
+———-+
[email protected]:/var/lib/mysql/mydb# ll -h

-rw-rw—- 1 mysql mysql 168M Sep 5 11:52 test.ibd
mysql> delete from test limit 2000000;
mysql> select count() From test; +———-+ | count() |
+———-+
| 1145728 |
+———-+
[email protected]:/var/lib/mysql/mydb# ll -h

-rw-rw—- 1 mysql mysql 168M Sep 5 11:52 test.ibd

mysql> select count() From test; +———-+ | count() |
+———-+
| 3145728 |
+———-+
[email protected]:/var/lib/mysql/mydb# ll -h

-rw-rw—- 1 mysql mysql 168M Sep 5 11:52 test.ibd

mysql> delete from test limit 2000000;
mysql> select count() From test; +———-+ | count() |
+———-+
| 1145728 |
+———-+

[email protected]:/var/lib/mysql/mydb# ll -h

-rw-rw—- 1 mysql mysql 168M Sep 5 11:52 test.ibd
You can see that after deleting 2M records, the test.ibd size was 168M.

Shell

mysql> optimize table test;
+———–+———-+———-+——————————————————————-+
| Table | Op | Msg_type | Msg_text |
+———–+———-+———-+——————————————————————-+
| mydb.test | optimize | note | Table does not support optimize, doing recreate + analyze instead |
| mydb.test | optimize | status | OK |
+———–+———-+———-+——————————————————————-+
[email protected]:/var/lib/mysql/mydb# ll -h

-rw-rw—- 1 mysql mysql 68M Sep 5 12:47 test.ibd

mysql> optimize table test;
+———–+———-+———-+——————————————————————-+
| Table | Op | Msg_type | Msg_text |
+———–+———-+———-+——————————————————————-+
| mydb.test | optimize | note | Table does not support optimize, doing recreate + analyze instead |
| mydb.test | optimize | status | OK |
+———–+———-+———-+——————————————————————-+

[email protected]:/var/lib/mysql/mydb# ll -h

-rw-rw—- 1 mysql mysql 68M Sep 5 12:47 test.ibd
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.