Documentation Home
MySQL 9.3 Reference Manual
Related Documentation Download this Manual
PDF (US Ltr) - 40.8Mb
PDF (A4) - 40.9Mb
Man Pages (TGZ) - 261.1Kb
Man Pages (Zip) - 368.3Kb
Info (Gzip) - 4.1Mb
Info (Zip) - 4.1Mb


17.11.4 Defragmenting a Table

Random insertions into or deletions from a secondary index can cause the index to become fragmented. Fragmentation means that the physical ordering of the index pages on the disk is not close to the index ordering of the records on the pages, or that there are many unused pages in the 64-page blocks that were allocated to the index.

One symptom of fragmentation is that a table takes more space than itshould take. How much that is exactly, is difficult to determine. AllInnoDB data and indexes are stored inB-trees, and theirfill factor may vary from 50% to 100%. Another symptom of fragmentation is that a table scan such as this takes more time than itshould take:

SELECT COUNT(*) FROM t WHEREnon_indexed_column <> 12345;

The preceding query requires MySQL to perform a full table scan, the slowest type of query for a large table.

To speed up index scans, you can periodically perform anullALTER TABLE operation, which causes MySQL to rebuild the table:

ALTER TABLEtbl_name ENGINE=INNODB

You can also useALTER TABLEtbl_name FORCE to perform anull alter operation that rebuilds the table.

BothALTER TABLEtbl_name ENGINE=INNODB andALTER TABLEtbl_name FORCE useonline DDL. For more information, seeSection 17.12, “InnoDB and Online DDL”.

Another way to perform a defragmentation operation is to usemysqldump to dump the table to a text file, drop the table, and reload it from the dump file.

If the insertions into an index are always ascending and records are deleted only from the end, theInnoDB filespace management algorithm guarantees that fragmentation in the index does not occur.