TechBeamersTechBeamers
  • Learn ProgrammingLearn Programming
    • Python Programming
      • Python Basic
      • Python OOP
      • Python Pandas
      • Python PIP
      • Python Advanced
      • Python Selenium
    • Python Examples
    • Selenium Tutorials
      • Selenium with Java
      • Selenium with Python
    • Software Testing Tutorials
    • Java Programming
      • Java Basic
      • Java Flow Control
      • Java OOP
    • C Programming
    • Linux Commands
    • MySQL Commands
    • Agile in Software
    • AngularJS Guides
    • Android Tutorials
  • Interview PrepInterview Prep
    • SQL Interview Questions
    • Testing Interview Q&A
    • Python Interview Q&A
    • Selenium Interview Q&A
    • C Sharp Interview Q&A
    • PHP Interview Questions
    • Java Interview Questions
    • Web Development Q&A
  • Self AssessmentSelf Assessment
    • Python Test
    • Java Online Test
    • Selenium Quiz
    • Testing Quiz
    • HTML CSS Quiz
    • Shell Script Test
    • C/C++ Coding Test
Search
  • Python Multiline String
  • Python Multiline Comment
  • Python Iterate String
  • Python Dictionary
  • Python Lists
  • Python List Contains
  • Page Object Model
  • TestNG Annotations
  • Python Function Quiz
  • Python String Quiz
  • Python OOP Test
  • Java Spring Test
  • Java Collection Quiz
  • JavaScript Skill Test
  • Selenium Skill Test
  • Selenium Python Quiz
  • Shell Scripting Test
  • Latest Python Q&A
  • CSharp Coding Q&A
  • SQL Query Question
  • Top Selenium Q&A
  • Top QA Questions
  • Latest Testing Q&A
  • REST API Questions
  • Linux Interview Q&A
  • Shell Script Questions
© 2024 TechBeamers. All Rights Reserved.
Reading: MySQL OPTIMIZE TABLE Statement
Font ResizerAa
TechBeamersTechBeamers
Font ResizerAa
  • Python
  • SQL
  • C
  • Java
  • Testing
  • Selenium
  • Agile
  • Linux
  • MySQL
  • Python Quizzes
  • Java Quiz
  • Testing Quiz
  • Shell Script Quiz
  • WebDev Interview
  • Python Basic
  • Python Examples
  • Python Advanced
  • Python OOP
  • Python Selenium
  • General Tech
Search
  • Programming Tutorials
    • Python Tutorial
    • Python Examples
    • Java Tutorial
    • C Tutorial
    • MySQL Tutorial
    • Selenium Tutorial
    • Testing Tutorial
  • Top Interview Q&A
    • SQL Interview
    • Web Dev Interview
  • Best Coding Quiz
    • Python Quizzes
    • Java Quiz
    • Testing Quiz
    • ShellScript Quiz
Follow US
© 2024 TechBeamers. All Rights Reserved.
MySQL Tutorial

MySQL OPTIMIZE TABLE Statement

Last updated: Feb 27, 2024 10:33 pm
By Meenakshi Agarwal
Share
4 Min Read
MySQL OPTIMIZE TABLE Statement with Examples
SHARE

This tutorial explains MySQL OPTIMIZE TABLE statement which defrags tables and recovers unused space. We’ll describe the complete usage of this method with the help of simple examples.

Contents
Check Tables for OptimizationMySQL OPTIMIZE TABLE CommandPost-MySQL Optimization

If your database is receiving a lot of deletes and updates calls, then it might lead to fragmentation in your MySQL data files.

Therefore, a lot of unused space would go in vain, and also put a high impact on the performance.

So, experts recommend that you must defrag your MySQL tables regularly. Hence, we’ll today explain how to use the MySQL OPTIMIZE TABLE to defrag tables and free up space.

MySQL OPTIMIZE TABLE

Before you do optimization, first confirm whether your MySQL database is suffering from fragmentation or not. To know it, run the below command.

Check Tables for Optimization

You need to analyze which table is consuming more space in your database. Hence, connect to the MySQL DB instance, and run the below query.

It should fetch the tables which are accounting for the unused space.

-- List all tables causing unused space
SELECT TABLE_NAME,
       ROUND(DATA_LENGTH/1024/1024) AS USED_SPACE_MB, 
       ROUND(DATA_FREE/1024/1024) AS UNUSED_SPACE_MB 
FROM INFORMATION_SCHEMA.TABLES 
WHERE ROUND(DATA_FREE/1024/1024) > 1000 
ORDER BY UNUSED_SPACE_MB;

After running the above SQL query, you shall see this type of result:

+------------+---------------+-----------------+
| TABLE_NAME | USED_SPACE_MB | UNUSED_SPACE_MB |
+------------+---------------+-----------------+
| EMPLOYEES  | 6917          | 5284            |
| SALESINFO  | 21473         | 11097           |
| FINANCES   | 11825         | 21286           |
+------------+---------------+-----------------+

We can interpret the following facts from the output:

  • First, the SELECT command is listing all tables that are causing more than 1000 MB of free space.
  • The columns USED_SPACE_MB and UNUSED_SPACE_MB are showing data in MB.
  • The results indicate that all three tables are a candidate for optimization as they are causing high fragmentation.

MySQL OPTIMIZE TABLE Command

This command uses the following syntax:

mysql> OPTIMIZE TABLE table1 [, table2 ] ...

We can use the above in one of the following ways:

First, we optimize one table using this MySQL statement.

mysql> OPTIMIZE TABLE EMPLOYEES;

Secondly, we can optimize multiple tables together, as shown below:

mysql> OPTIMIZE TABLE EMPLOYEES, SALESINFO, FINANCES;

While running optimization on a table, MySQL does the following tasks:

  • Creates a temp table,
  • Deletes the original one after optimizing it, and
  • Rename the temp table to the original name in the end.

Post-MySQL Optimization

After finishing up with optimization, you can issue the below command. It will fetch the size of the total as well as the unused-space the three tables are claiming.

-- Query tables we'd optimized
SELECT TABLE_NAME,
       ROUND(DATA_LENGTH/1024/1024) AS USED_SPACE_MB, 
       ROUND(DATA_FREE/1024/1024) AS UNUSED_SPACE_MB 
FROM INFORMATION_SCHEMA.TABLES 
WHERE TABLE_NAME in  
('EMPLOYEES', 'SALESINFO', 'FINANCES');

After running the above SQL query, you shall see this type of result:

+------------+---------------+-----------------+
| TABLE_NAME | USED_SPACE_MB | UNUSED_SPACE_MB |
+------------+---------------+-----------------+
| EMPLOYEES  | 3791          | 0               |
| SALESINFO  | 10012         | 0               |
| FINANCES   | 11005         | 0               |
+------------+---------------+-----------------+

You can easily deduce from the outcome that MySQL OPTIMIZE TABLE command has significantly reduced the size. And, unused space is no more.

Also, the table sizes have come down too. And it helped us fix a lot of fragmentation at the filesystem level.

Anyways, we hope that after wrapping up this tutorial, you should feel comfortable in optimizing the tables. However, you may practice more with examples to gain confidence.

Also, to learn SQL from scratch to depth, do read our step by step MySQL tutorial.

You Might Also Like

MySQL vs MongoDB Comparison

Concatenate Strings in an SQL Query With Examples

The Difference between UPSERT & Insert

SQL Programming Test in 2024

SQL Table Creation: The Missing Manual

Sign Up For Daily Newsletter

Be keep up! Get the latest breaking news delivered straight to your inbox.
Loading
By signing up, you agree to our Terms of Use and acknowledge the data practices in our Privacy Policy. You may unsubscribe at any time.
Meenakshi Agarwal Avatar
By Meenakshi Agarwal
Follow:
Hi, I'm Meenakshi Agarwal. I have a Bachelor's degree in Computer Science and a Master's degree in Computer Applications. After spending over a decade in large MNCs, I gained extensive experience in programming, coding, software development, testing, and automation. Now, I share my knowledge through tutorials, quizzes, and interview questions on Python, Java, Selenium, SQL, and C# on my blog, TechBeamers.com.
Previous Article MySQL CONCAT to Concatenate Strings MySQL CONCAT to Concatenate Strings
Next Article MySQL Aggregate Functions with Examples MySQL Aggregate Functions

Popular Tutorials

SQL Interview Questions List
50 SQL Practice Questions for Good Results in Interview
SQL Interview Nov 01, 2016
Demo Websites You Need to Practice Selenium
7 Sites to Practice Selenium for Free in 2024
Selenium Tutorial Feb 08, 2016
SQL Exercises with Sample Table and Demo Data
SQL Exercises – Complex Queries
SQL Interview May 10, 2020
Java Coding Questions for Software Testers
15 Java Coding Questions for Testers
Selenium Tutorial Jun 17, 2016
30 Quick Python Programming Questions On List, Tuple & Dictionary
30 Python Programming Questions On List, Tuple, and Dictionary
Python Basic Python Tutorials Oct 07, 2016
//
Our tutorials are written by real people who’ve put in the time to research and test thoroughly. Whether you’re a beginner or a pro, our tutorials will guide you through everything you need to learn a programming language.

Top Coding Tips

  • PYTHON TIPS
  • PANDAS TIPSNew
  • DATA ANALYSIS TIPS
  • SELENIUM TIPS
  • C CODING TIPS
  • GDB DEBUG TIPS
  • SQL TIPS & TRICKS

Top Tutorials

  • PYTHON TUTORIAL FOR BEGINNERS
  • SELENIUM WEBDRIVER TUTORIAL
  • SELENIUM PYTHON TUTORIAL
  • SELENIUM DEMO WEBSITESHot
  • TESTNG TUTORIALS FOR BEGINNERS
  • PYTHON MULTITHREADING TUTORIAL
  • JAVA MULTITHREADING TUTORIAL

Sign Up for Our Newsletter

Subscribe to our newsletter to get our newest articles instantly!

Loading
TechBeamersTechBeamers
Follow US
© 2024 TechBeamers. All Rights Reserved.
  • About
  • Contact
  • Disclaimer
  • Privacy Policy
  • Terms of Use
TechBeamers Newsletter - Subscribe for Latest Updates
Join Us!

Subscribe to our newsletter and never miss the latest tech tutorials, quizzes, and tips.

Loading
Zero spam, Unsubscribe at any time.
x