7

MySQL 5.1.4x (Windows) | Innodb

I recently purged data from a mySQL DB (a few hundred thousand rows) and I'm planning to use

OPTIMIZE TABLE LOGTABLEFOO1,LOGTABLEFOO2,LOGTABLEFOO3;

to reduce the data space footprint the empty whitespace is taking up in the filesystem.

My concern is that once I start this command, I won't know how long it's going to take or where it is in the process. Is there anyway I can determine this information? As far as I know, there's no progress indicator.

4 Answers 4

12

There is nothing MySQL has that will indicate the progress of OPTIMIZE TABLE;.

You may have to go the OS using Windows Explorer and look for a growing tmp table. Hint: Temp table are MyISAM tables that do not have a .frm file with the name #sql-9999.MYD and #sql-9999.MYI. While these two fle exist, the query would be considered in progress. To know how far, one would have to know how many rows times the average row length to estimate the percentage done or left to be done.

MariaDB has some progress metering for the following:

  • ALTER TABLE
  • ADD INDEX
  • DROP INDEX
  • LOAD DATA INFILE
  • CHECK TABLE
  • REPAIR TABLE
  • ANALYZE TABLE
  • OPTIMIZE TABLE

I have discussed subject before in terms of MySQL operations

If you are trying to reduce the size of ibdata1 for InnoDB, dropping tables simply leaves big gaps in ibdata1. You must perform a full Cleanup of InnoDB Infrastructure.

UPDATE 2012-12-21 19:30 EDT

Since this database uses innodb, will there be a growing temp table I can look for?

Let's say you want to run

OPTIMIZE TABLE mydb.mytable;

Look at two scenarios involving innodb_file_per_table and this OPTIMIZE TABLE

SCENARIO #1 : innodb_file_per_table disabled

Running OPTIMIZE TABLE will cause all the data and index pages for mydb.mytable to be appended contiguously to ibdata1. This will leave the space previously occupied to add further to the fragmentation madness.

In this scenario, the temp table would exist as InnoDB, and it would materialize in ibdata1. Thus, there would be nothing to visually monitor.

SCENARIO #2 : innodb_file_per_table enabled

For each InnoDB table mydb.mytable you would have

  • /var/lib/mysql/mydb/mytable.frm
  • /var/lib/mysql/mydb/mytable.ibd

The .ibd file contains the data and index pages for the table.

Running OPTIMIZE TABLE will cause all the data and index pages for mydb.mytable to be written to an external .ibd file that you can see in Windows Explorer.

CAVEAT

Enabling innodb_file_per_table and running OPTIMIZE TABLE will never reclaim the space formerly occupied within ibdata1. You will have to carry out the InnoDB Cleanup to recreate ibdata1 in such a way that data and indexes never, ever again reside in ibdata1.

CONCLUSION

As stated earlier, MariaDB can monitor OPTIMIZE TABLE;

If you want something you can monitor and you have innodb_file_per_table enabled, you can replace

OPTIMIZE TABLE LOGTABLEFOO1,LOGTABLEFOO2,LOGTABLEFOO3;

with the following mechanical equivalent:

CREATE TABLE LOGTABLEFOO1_NEW LIKE LOGTABLEFOO1;
INSERT INTO LOGTABLEFOO1_NEW SELECT * FROM LOGTABLEFOO1;
ALTER TABLE LOGTABLEFOO1 RENAME LOGTABLEFOO1_OLD;
ALTER TABLE LOGTABLEFOO1_NEW RENAME LOGTABLEFOO1;
DROP TABLE LOGTABLEFOO1_OLD;
ANALYZE TABLE LOGTABLEFOO1;

CREATE TABLE LOGTABLEFOO2_NEW LIKE LOGTABLEFOO2;
INSERT INTO LOGTABLEFOO2_NEW SELECT * FROM LOGTABLEFOO2;
ALTER TABLE LOGTABLEFOO2 RENAME LOGTABLEFOO2_OLD;
ALTER TABLE LOGTABLEFOO2_NEW RENAME LOGTABLEFOO2;
DROP TABLE LOGTABLEFOO2_OLD;
ANALYZE TABLE LOGTABLEFOO2;

CREATE TABLE LOGTABLEFOO3_NEW LIKE LOGTABLEFOO3;
INSERT INTO LOGTABLEFOO3_NEW SELECT * FROM LOGTABLEFOO3;
ALTER TABLE LOGTABLEFOO3 RENAME LOGTABLEFOO3_OLD;
ALTER TABLE LOGTABLEFOO3_NEW RENAME LOGTABLEFOO3;
DROP TABLE LOGTABLEFOO3_OLD;
ANALYZE TABLE LOGTABLEFOO3;

Please keep in mind the OPTIMIZE TABLE is nothing but ALTER TABLE ... ENGINE=InnoDB; followed by ANALYZE TABLE.

With these steps, you know in advance what the temp tables are

  • LOGTABLEFOO1_NEW.ibd
  • LOGTABLEFOO2_NEW.ibd
  • LOGTABLEFOO3_NEW.ibd

Simply Monitor Each File's Existence and Size Until it Disappears.

0
2

There is a simpler way, if you have access to the file system.

MySQL generates a .TMD file; you can watch the size of the file growing; it should reach more or less the size of your existing MYD when it is done.

For instance, if you cd to your datadir, and simply watch "ls -lah|grep table_name" which will return something like this (on Linux). In my case, the progress is 287 MB out of 427 MB (roughly).

-rw-rw----.  1 mysql mysql 427M Feb 19 23:45 ticket_change.MYD
-rw-rw----.  1 mysql mysql 541M Feb 20 01:10 ticket_change.MYI
-rw-rw----.  1 mysql mysql 287M Feb 20 01:45 ticket_change.TMD

1
  • Welcome on the DBA SE! Note, please don't use screenshots, if also text copy-paste is enough.
    – peterh
    Feb 20, 2019 at 18:42
1

Percona Toolkit's pt-online-schema-change does this for you.

Change mydb.LOGTABLEFOO1 to InnoDB, effectively performing OPTIMIZE TABLE in a non-blocking fashion because it is already an InnoDB table

pt-online-schema-change --alter "ENGINE=InnoDB" D=mydb,t=LOGTABLEFOO1
1

You can watch the size of the table by using this SQL from another unrelated MySQL docs:

SELECT NAME, FS_BLOCK_SIZE, FILE_SIZE, ALLOCATED_SIZE FROM INFORMATION_SCHEMA.TABLES 
WHERE NAME = 'scheme/table_name';

remember the value of fields FILE_SIZE and/or ALLOCATED_SIZE(they will vary in a large range when the table is using InnoDB Page Compression) in its output before starting to execute OPTIMIZE TABLE:

NAME FS_BLOCK_SIZE FILE_SIZE ALLOCATED_SIZE
scheme/table_name 4096 9776922624 4889554944

Then re-run this SQL regularly, you should notice these values have been reset to zero and then growing slowly until the OPTIMIZE is finished.

For approx. progress will be current size/original size before OPTIMIZE, note this will be less or larger than 100% when it's finished since the size of the rebuilt table might be larger or smaller than the original one.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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