2

I inherited a MySQL InnoDB table that has approximately 200 GB worth of data, as well as several unneeded indices that amount to approximately 500 GB. I've archived approximately 75 percent of the records off site and DELETED the records from my table, and would like to reclaim the space in the table. I'd like to know if I am going to need about 200 GB of free space in order to reclaim the space, or closer to 700 GB.

innodb_file_per_table is on.

MySQL version is 5.5.

And I am considering either running OPTIMIZE TABLE (eg Recover the disk space after deleting rows from table) or using Percona Toolkit's online schema change (eg http://www.percona.com/blog/2013/09/25/how-to-reclaim-space-in-innodb-when-innodb_file_per_table-is-on/). I'd prefer to do the later, because I can avoid write locks, but I'd use OPTIMIZE TABLE if there was a considerable space difference required.

Will I need closer to 200 gb or close to 700 gb of free space in order to reclaim space from InnoDB? If closer to 700GB, would that answer be any different if I first converted the table to MyISAM (eg http://www.percona.com/blog/2014/08/21/the-mysql-ibdata1-disk-space-issue-and-big-tables-part-1/)?

Thank you for your help!

1

DISKSPACE FOR EVERYTHING INNODB

SELECT FORMAT(SUM(data_length+index_length)/POWER(1024,3),2) InnoDB_DiskSpace
FROM information_schema.tables
WHERE engine='InnoDB';

DISKSPACE FOR DATABASE mydb BY TABLE

SELECT
    IFNULL(tbl,'Total') table_name,
    FORMAT(SUM(table_bytes)/POWER(1024,3),2) table_size
FROM
(
    SELECT table_name tbl,SUM(data_length+index_length)table_bytes
    FROM information_schema.tables WHERE table_schema='mydb
    GROUP BY table_name WITH ROLLUP
) A ORDER BY ISNULL(tbl) DESC,table_bytes;

DISKSPACE FOR INDIVIDUAL TABLE (mydb.mytable)

SELECT FORMAT(SUM(data_length+index_length)/POWER(1024,3),2) Table_Diskspace
FROM information_schema.tables
WHERE table_schema='mydb;
AND table_name='mytable';

These queries will tell you how much space you need for any table, database, and engine.

SUGGESTION #1

If you would like to simulate OPTIMIZE TABLE just to see diskspace usage, you run the following steps on mydb.mytable

use mydb
CREATE TABLE mytable_new LIKE mytable;
INSERT INTO mytable_new SELECT * FROM mytable;
ANALYZE TABLE mytable_new;
SELECT FORMAT(SUM(data_length+index_length)/POWER(1024,3),2) Table_Diskspace
FROM information_schema.tables
WHERE table_schema='mydb;
AND table_name='mytable_new';
#
# Stop Here. If you are happy with the size, do the last two commands
# If you prefer, drop table mytable_new and do do the online schema change
#
RENAME TABLE mytable TO mytable_old,mytable_newTO mytable;
DROP TABLE mytable_old;

Doing this can give an idea who big the .ibd file will be after running OPTIMIZE TABLE.

SUGGESTION #2

Please do not convert the table to MyISAM. It will be building two files (.MYD and .MYI). Then, you try to converting to InnoDB again. It is essentially the same as doing #SUGGESTION #1. MyISAM is also a little more prone to corruption with no recovery processes in place.

EPILOGUE

By all means, you can proceed with pt-online-schema-change if your data is live. My answer simply gives you queries to estimate sizes of the tables and approximate temp table sizes for OPTIMIZE TABLE.

If you are doing this during off hours, try my post Recover the disk space after deleting rows from table

GIVE IT A TRY !!!

  • Hi Rolando, thanks for this detailed explanation. My concern is I'd really like to estimate how much space I'm going to need to pull this change off before actually using up that much space. The current table takes up about 700GB. I've deleted about 75 percent of the records. If I have 500 GB of free space, will that be enough? Or could it use all 700 gb before it manages to reclaim the space of the deleted records? – evanv Nov 23 '14 at 2:09
  • 1
    My answer gives you an idea how big a table will be during and after OPTIMIZE TABLE. Just try the queries on a couple of midsize tables that need shrinking. If the temp tables have a good size, then my queries will accurate predict how much space you will need. – RolandoMySQLDBA Nov 23 '14 at 2:16

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.