Optimizing tables and databases in MySQL
This tutorial is an exhaustive guide on how to optimize tables and databases in MySQL. We will be using two operating systems, Windows and Linux (Ubuntu 20.04).
It also introduces the importance of optimization in MySQL.
When and Why to Optimize Tables in MySQL
If our application performs many DELETE
and UPDATE
operations on the database, there are higher chances of data files fragmentation in MySQL. It results in unused space which also affects the performance.
There is a great need to continuously defragment MySQL tables and reclaim unused space. This is where we need table optimization in MySQL, which enables reordering of data in dedicated storage servers, ultimately improving the performance and speed of data input and output.
Now, how do we know which table we should optimize? We should optimize tables where information (data) is constantly updated; for example, a transactional database is a perfect candidate for table optimization.
However, optimizing the query may consume more time, depending on the size of the table and database. Therefore, it is not good for the transaction system to lock the table for many hours.
Instead, we can INNODB
try a few tricks in the Engine table. Here are some of them:
Optimizing tables in MySQL
First, we should analyze the table we want to optimize. We have to connect to our database using the following command.
Sample code:
-- Syntax: Use your_database_name;
mysql> USE test;
Once connected with the required database, use the following query to get the status of the table.
Sample code:
-- Syntax: SHOW TABLE STATUS LIKE "your_table_name" \G
mysql> SHOW TABLE STATUS LIKE "test_table" \G
Output:
We have two important properties to know if we should optimize this table.
This information guides us in determining which tables need to be optimized and the amount of space we will reclaim afterwards.
Data_length
We can get these two numbers ( and )
for all tables in a particular database using the following query Data_free
. Currently, we test
have only one test_table
table named in the database.
Sample code:
mysql> SELECT TABLE_NAME, data_length, data_free
-> FROM information_schema.tables
-> WHERE table_schema='test'
-> ORDER BY data_free DESC;
Output:
The above query prints the name of the table, total space in bytes, and allocated unused space in bytes. If you are comfortable working in MB, you can use the following query to get the output in MB.
Sample code:
mysql> SELECT TABLE_NAME,
-> round(data_length/1024/1024) AS Data_Length_in_MBs,
-> round(data_free/1024/1024) AS Data_Free_in_MBs
-> FROM information_schema.tables
-> WHERE table_schema='test'
-> ORDER BY data_free DESC;
Although the given example table is not severely fragmented, we can OPTIMIZE TABLE
reclaim space using the command.
We will learn to optimize single/multiple tables or databases using MySQL shell and terminal.
Optimize single or multiple tables using MySQL Shell in Windows/Ubuntu 20.04 operating system
Sample code:
mysql> OPTIMIZE TABLE test_table;
Output:
To optimize multiple tables, use the following query.
Sample code:
mysql> OPTIMIZE TABLE tableName1, tableName2;
Now, confirm that the required tables are optimized using the following command.
Sample code:
mysql> SELECT TABLE_NAME, data_length, data_free
-> FROM information_schema.tables
-> WHERE table_schema='test'
-> ORDER BY data_free DESC;
Output:
Optimizing tables using Windows Terminal
Sample code:
-- Syntax mysqlcheck -o <schema> <table> -u <username> -p <password>
mysqlcheck -o test test_table -u root -p
Once we write the command given above and hit enter Enter, we will be asked for the MySQL root
password. Enter that.
We must be bin
in the folder to perform the query (see the example below).
Sample code:
C:\Program Files\MySQL\MySQL Server 8.0\bin>mysqlcheck -o test test_table -u root -p
Enter password: *****
Optimize tables using Ubuntu 20.04 terminal
If we log in sudo su
as mysql using command 超级用户
and execute the query given below. It will only ask for MySQL root
password.
Sample code:
mysqlcheck -o test test_table -u root -p
If we are not 超级用户
logged in as , we will execute mysqlcheck
the command. Here we will be asked for the system's root
and MySQL root
passwords.
Sample code:
sudo mysqlcheck -o test test_table -u root -p
Optimize all tables in a MySQL database
To optimize all tables using the Windows command line:
-- Syntax: mysqlcheck -o your_database_name -u username -pPassword
mysqlcheck -o test -u root -p
Use the Ubuntu terminal to optimize all tables:
-- if you are signed in as a superuser
mysqlcheck -o test -u root -p
-- if you are not signed in as a superuser
sudo mysqlcheck -o test -u root -p
Optimize all databases in MySQL
To optimize all databases using the Windows command line:
-- Syntax: mysqlcheck -o --all-databases -u username -pPassword
mysqlcheck -o --all-databases -u root -p
Use the Ubuntu terminal to optimize all databases:
-- if you are signed in as a superuser
mysqlcheck -o --all-databases -u root -p
-- if you are not signed in as a superuser
sudo mysqlcheck -o --all-databases -u root -p
For reprinting, please send an email to 1244347461@qq.com for approval. After obtaining the author's consent, kindly include the source as a link.
Related Articles
How to select from multiple tables in MySQL
Publish Date:2025/04/25 Views:57 Category:MySQL
-
This article explains how to use MySQL to query from multiple tables in one script SELECT . Let's demonstrate a situation: SELECT name , price, details, type , FROM food, food_order WHERE breakfast.id = 'breakfast_id' Now, let's imagine FRO
Creating a table from CSV in MySQL
Publish Date:2025/04/25 Views:114 Category:MySQL
-
In this article, we aim to understand how to create a table from CSV in MySQL database. Businesses and organizations must quickly generate tables from large amounts of data. These organizations typically have large CSV files with large amou
Creating a Temporary Table in MySQL
Publish Date:2025/04/25 Views:183 Category:MySQL
-
In this article, we aim to explore different ways to create temporary tables in MySQL. One of the main features of temporary tables is that it helps in storing temporary data. This feature is enabled in MySQL 3.23 and later versions. These
Truncate all tables in Mysql
Publish Date:2025/04/25 Views:89 Category:MySQL
-
Today I will show you how to truncate all tables in Mysql. It is used when you want to delete the entire table TRUNCATE TABLE . TRUNCATE It is a type of DML statement, which means it cannot be rolled back once it is committed. There are two
Different ways to check if a row exists in a MySQL table
Publish Date:2025/04/25 Views:163 Category:MySQL
-
This article highlights different ways to check if a row exists in a MySQL table. We will use the EXISTS and NOT EXISTS operators. We can also use these two operators with IF() the function to get a meaningful message if a row (a record) is
Check if table exists in MySQL
Publish Date:2025/04/25 Views:194 Category:MySQL
-
This article provides several options to check if a table exists in MySQL. Before discussing it, let us first see what a table is in MySQL and when you need to check its existence. What is a table in MySQL? A table is a database object that
Rename columns in MySQL database
Publish Date:2025/04/25 Views:80 Category:MySQL
-
In this article, we aim to explore different ways to rename columns in MySQL. ALTER TABLE The command is mainly used to change the format of a given MySQL table. It can be used to add columns, change the data type within a column, delete co
Copying a table in MySQL
Publish Date:2025/04/25 Views:142 Category:MySQL
-
The purpose of this article is to explore different ways to create a copy of a table in MySQL. The source table is also called the table to be copied, and the target table is called the clone table, which can be from the same or a different
Get column names in MySQL
Publish Date:2025/04/25 Views:109 Category:MySQL
-
In this article, we aim to explore how to get the column names of a specific table in MySQL database. Often, when working with data in MySQL, we tend to forget the column names of a particular table in the database and the data types of dif