25

I have read several times that after you delete a row in an InnoDB table in MySQL, its space is not reused, so if you make a lot of INSERTs into a table and then periodically DELETE some rows the table will use more and more space on disk, as if the rows were not deleted at all.

Recently I've been told though that the space occupied by deleted rows is re-used but only after some transactions are complete and even then - not fully. I am now confused.

Can someone please make sense of this to me? I need to do a lot of INSERTs into an InnoDB table and then every X minutes I need to DELETE records that are more than Y minutes old. Do I have a problem of ever-growing InnoDB table here, or is it paranoia?

Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055

2 Answers2

36

It is paranoia :)

DB's don't grow in size unnecessarily, but for performance issues space is not freed either.

What you've heard most probably is that if you delete records that space is not given back to the Operating System. Instead, it's kept as an empty space for the DB to re-use afterwards.

This is because:

  • DB needs to have some HD space to save its data; if it doesn't have any space, it reserves some empty space at first.
  • When you insert a new row, a piece of that space is used.
  • When you run out of free space, a new block is reserved, and so on.
  • Now, when you delete some rows, in order to prevent reserving more and more blocks, its space is kept free but never given back to the Operating System, so you can use it again later without any need of reserving new blocks.

As you can see, space is re-used, but never given back. That's the key point to your question.

Seb
  • 24,920
  • 5
  • 67
  • 85
  • Seb, can you please given any official link for this? – Gautam Somani May 26 '15 at 11:20
  • 2
    @GautamSomani This is a 6 years old answer, I don't have links at hand but I could find this: https://dev.mysql.com/doc/refman/5.1/en/optimize-table.html: `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. After extensive changes to a table, this statement may also improve performance of statements that use the table, sometimes significantly.` I'm sure if you google you'll be able to find more official info about this. – Seb May 27 '15 at 17:26
2

in innodb, there is no practical way of freeing up the space.

All of these methods become impractical when you are using huge tables(in my case they are more than 250GB) and you must keep them deleting records to better performance.

You will have to seriously think, whether you have enough space on your harddisk to perform one of the above function (in my case I do not think 1TB is enough for all these actions)

with Innotab table (and mysql itself) the option are fairly limited if have serious database size.

  • There are solutions to this, with newer InnoDB versions and the right configuration (`innodb_file_per_table`): http://dev.mysql.com/doc/innodb-plugin/1.0/en/innodb-other-changes-truncate.html http://dev.mysql.com/doc/refman/5.5/en/innodb-truncate-table-reclaim-space.html – Blaisorblade Jan 26 '14 at 12:31