Databases 2 min read

How to Detect and Remove MySQL Table Fragmentation for Faster Queries

This guide explains why MySQL tables become fragmented after deletions, how to check the Data_free value to identify fragmented tables, and which commands to run for MyISAM and InnoDB tables to reclaim space and improve query performance.

Java High-Performance Architecture
Java High-Performance Architecture
Java High-Performance Architecture
How to Detect and Remove MySQL Table Fragmentation for Faster Queries

Table storage can become fragmented when rows are deleted, leaving empty space that MySQL still scans, degrading query performance.

Viewing Fragmentation Size

Run SHOW TABLE STATUS LIKE 'table_name'; and check the Data_free column for the amount of free space (fragmentation).

Listing All Fragmented Tables

Execute the following query to list schemas with non‑zero data_free:

select table_schema db, table_name, data_free, engine
from information_schema.tables
where table_schema not in ('information_schema','mysql')
  and data_free > 0;

Removing Fragmentation

MyISAM tables : OPTIMIZE TABLE table_name; InnoDB tables :

ALTER TABLE table_name ENGINE=InnoDB;

Recommendations

Fragment removal locks the table temporarily; larger data takes longer. Automate the process with a script that runs during low‑traffic periods (e.g., early Wednesday mornings) and cleans tables whose Data_free exceeds a chosen threshold.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

performanceoptimizationmysqlDatabase Maintenancetable fragmentation
Java High-Performance Architecture
Written by

Java High-Performance Architecture

Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.

0 followers
Reader feedback

How this landed with the community

Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.