How I Find MySQL FragmentationSarah Novotny recently posted InnoDB Tablespace Fragmentation - Find it and Fix it, so I thought I would share how I determine MySQL fragmentation. This works for MyISAM and InnoDB tables, and will work whether or not innodb_file_per_table is in use.The basic idea is to compare the size of the file(s) on disk with the size in the database metadata. The DATA_LENGTH and INDEX_LENGTH fields of the TABLES table in the INFORMATION_SCHEMA database has size information calculated by the storage engine. There are a few points to note:
SELECT (DATA_LENGTH+INDEX_LENGTH)/1024/1024 AS sizeMb FROM INFORMATION_SCHEMA.TABLES WHERE ENGINE='MyISAM';
SELECT SUM(DATA_LENGTH+INDEX_LENGTH)/1024/1024 AS sizeMb FROM INFORMATION_SCHEMA.TABLES WHERE ENGINE='InnoDB';
SELECT (DATA_LENGTH+INDEX_LENGTH)/1024/1024 AS sizeMb FROM INFORMATION_SCHEMA.TABLES WHERE ENGINE='InnoDB'; Note that every database is different. I use the 20% as a guideline, not a hard-and-fast rule. It really depends on how "wrong" Innodb's estimate of the size is. Fragmentation occurs when variable-length data is updated, and when variable-length rows are deleted. You can probably guess which tables have the most fragmentation if you know what queries are being run. If you want to get a better sense of how often updates and deletes are being run, I highly recommend using mk-query-digest against a sample set of binary logs.
|
Follow me on:SearchNavigation |