How to Prevent Innodb Data File Keep Growing
January 18th, 2008 by Andrew Chen
I believe one of the areas MySQL server need to improve when competing with other database software is that it needs to provide an easy way to release not used space of the innodb file. MS SQL server is far more superior in that regards.
In MySQL the default setting is to have only one innodb file doesn’t matter how many databases you create on the server.
So in production environment you will have to watching the innodb file closely and don’t let it grow out of control. One way to prevent innodb data keep growing is to change the default setting so that every table has their own innodb table space. This basically solve the problem that innodb doesn’t release non-used space when a table is dropped because with that setting turn on the innodb file for the table is deleted when you drop an innodb table. To turn on innodb file per table setting, just add innodb-file-per-table=ON to the config file. The existing tables will not be affecting by this setting though. The new innodb tables will have their own innodb file when you turn this setting on.
What if you never drop the table? If you keep inserting data to the table but once a while you delete some old data, innodb file will still keep growing. MySQL server is clever enough to reuse the empty space in the innodb data file only when empty space is not fragmented and usually deleting old data will leave fragment empty space in data file. I ever consult with MySL support about how to solve this problem. Here is what they say
If you are deleting most of a table at one time and leaving very few rows, it would be better to create a new table with the rows you want to keep and drop the old table. Or, you could schedule maintenance to do OPTIMIZE TABLE on the relatively small table left over after the large DELETE.
I have tried the OPTIMIZE command. It took very long time to finish for big tables that are over 10G it doesn’t seem to be a workable solution in production environment.
If you’re deleting rows based on date, and the rows have a sequential PK, fragmentation should be less of an issue as you’ll be deleting large chunks of sequential rows. If you have a non-sequential PK, on the other hand, fragmentation could be a pretty significant problem in that situation.
So if you are lucky your table has a sequential PK like auto increment number. You can delete old data based on date and MySQL server can reuse the empty space so that innodb data file will not keep growing.


Hi, the innodb file size was a night mare to me too.. and with about a 500 databases on one production server, I gave it a chance to write a set of shell scripts http://www.saturn.in/gpl/mysql.html
thanks for sharing
verified this by creating a temporary table of ~1.7GB size … innodb file size (on the filesystem) did not change
I see the main problem of mysql innodb is that operation can not be parallized (like optimize). This makes it unusabe for large databases with high activity.
[…] 参考: How to Prevent Innodb Data File Keep Growing 分类: computer 标签: innodb, mysql 评论 (0) Trackbacks (0) 发表评论 Trackback […]