How to reclaim space in InnoDB when innodb_file_per_table is ON

Editor’s Note:  This post was written for MySQL 5.1/5.5 and that the default for innodb_file_per_table was turned ON in 5.6 and the behaviors/process noted below may not be necessary for someone running 5.6/5.7.

When innodb_file_per_table is OFF and all data is going to be stored in ibdata files. If you drop some tables and delete some data then there is no 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 its 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.

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 free up MySQL tables in use 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:

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 the old table to the new one. In this process, a new .ibd tablespace will be created and 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 the performance when you’ll have a large table. So if you don’t want to lock the table then you can use one of the best utilities by Percona, pt-online-schema-change. It can ALTER without locking tables. You can run ALTER TABLE with ENGINE=INNODB which will re-create the table and reclaim the space.

(It’s always recommended to use the latest version of pt-* utilities)

Same here, you can notice that test .ibd file size decreased from 157M to 56M.

NOTE: Please make sure that you have ample space before you run pt-online-schema-change because it will create a temporary table that contains roughly the size of the original table. By running this on the primary node, the changes will be replicated to your slaves.

Share this post

Comments (12)

  • Jason

    Nice use of pt-online-schema-change, Thanks for one of the obvious but very often forgotten uses: plain old alter to clean up space!

    September 25, 2013 at 2:15 am
  • Ricardo

    Unfortunately, optimize table is not a good solution in general. Try to do that with a table with 50M rows, it will take you several hours if not days. Innodb should really fix it at the engine level, i have tables with triple the size of what they should be taking multi GB of data because of this inefficiency of innodb.

    September 25, 2013 at 2:27 pm
  • Nilnandan Joshi

    Agree Recardo, but until they fix this on engine level, we have to be rely on alternative solutions. For the bigger table, you can also use pt-online-schema-change which will run without locking tables.

    September 25, 2013 at 10:28 pm
  • Gerrit Tamboer

    Hi Joshi,
    Question: How does pt-online-schema-change handle the fact that while performing the action the source table might change (because it is not locked). Does it read the binlogs for changed data in the table?

    January 30, 2014 at 8:28 am
  • Nilnandan Joshi

    Hi Gerrit,

    As you know, pt-online-schema-change creates an empty copy of the table to alter, modifying it as desired, and then copying rows from the original table into the new table. So while copying into new table, pt-online-schema-change will create triggers on the original table to update the corresponding rows in the new table. So any modifications to data in the original tables during the copy will be reflected in the new table.

    Please check more information here.

    January 30, 2014 at 9:52 am
  • Josh

    Nifty use of the tool.

    Is it possible to force pt-online-schema-change to use an alternate partition or some other disk location to store the temporary table? I have a slightly degenerate case where we’re writing and deleting a massive amount of binary data to a single table. We use file_per_table, but it gets pretty easy to where the size of just that one table exceeds the remaining disk space left on the high speed SSD disk that this DB runs on.

    Is it possible to force the non-locking schema change to write the temp table to a slower disk of my choosing and then copy it back across when it’s been rewritten? Other thoughts on how we could handle?

    Thanks for a nice and useful post!

    March 6, 2014 at 9:45 am
  • Andreas

    Looks good!

    We need to reclaim disk space on one of our replication slaves a.s.a.p., which is why I am wondering if I can run pt-online-schema-change ONLY on the slave lacking disk space, or if that will mess up the replication all together (using row based replication)?

    If not, would it be possible to run pt-online-schema-change on the master and rely on the replication to reflect the changes (i.e. the file shrinkage) accordingly to the slave?


    September 15, 2014 at 11:21 am
  • Nilnandan Joshi

    Hi Andreas,

    With row based replication, you can’t use pt-osc (pt-online-schema-change) directly on slave because pt-osc uses triggers to get new data during update. triggers are not executed for ROW events, pt-osc will not work if slave will receive row event from master and you might get data inconsistency between master-slave servers.

    So in this case, you can use simple ALTER TABLE on slave directly, it can lock the table while ALTER running and slave can be on hold but you can reclaim the space.

    September 16, 2014 at 1:01 am
  • Carl Partridge

    What does pt-online-schema-change do about foreign keys? For example, if the table you are optimizing has foreign key references to another table, are they preserved?

    November 27, 2014 at 3:30 am
  • Jitendra

    Hello Nilnandan,

    I have a problem. The production table I want to alter is around 650 Gb. I want to remove a FK from that table. Mysql version is 5.1.61-log

    I used this tool for stage and that work fine. But the problem is I am not on innodb_file_per_table mode and on production server I am about 200g short on space, since table size is 650 GB and available space is 450 GB. There is no room for another drive.

    Please advise. Is there any way to skip copy?

    October 7, 2015 at 11:07 am
  • Erik Brakkee

    For a practical application of an automatic reclaim of table space one solution is to simply do the pt-online-schema-change for all tables with the ‘no-op’ alter table statement as mentioned in the post.

    However, this is not that efficient because that requires all data from all tables to be copied with possibly very little space reclaimed. As an alternative it would be nice to estimate beforehand how much space can be reclaimed for a particular table in relation to the total table size. Then the automatic task for reclaiming table space could run only when 10% or more table space can be reclaimed.

    To implement something like this it is necessary to get estimates for the total table size and the size of unused or used data in the table. Are there easy methods to do this in MySQL?

    February 24, 2016 at 9:56 am
  • rafero

    he, pt-online-schema-change about, if use this command, the size off the single file idata1 is diminuate ?

    April 6, 2017 at 5:04 am

Comments are closed.

Use Percona's Technical Forum to ask any follow-up questions on this blog topic.