View database and table data size in MySQL
When managing and maintaining a MySQL database, it is very important to understand the data size of the database and tables. This can help you monitor database growth, optimize performance, and plan storage needs. This blog will introduce how to use SQL queries to view the data size of MySQL databases and tables.
Check the total data size of MySQL database
First, we will learn how to view the total data size of the entire MySQL database, including all databases. The following is an example SQL query:
-- 显示所有数据库的总计
SELECT
'总和' AS `数据库`,
'' AS `表名`,
CONCAT(ROUND(SUM(table_rows) / 1000000, 2), 'M') AS `行数`,
CONCAT(ROUND(SUM(data_length) / (1024 * 1024 * 1024), 2), 'GB') AS `数据大小`,
CONCAT(ROUND(SUM(index_length) / (1024 * 1024 * 1024), 2), 'GB') AS `索引大小`,
CONCAT(ROUND((SUM(data_length) + SUM(index_length)) / (1024 * 1024 * 1024), 2), 'GB') AS `总大小`
FROM
information_schema.TABLES
WHERE
table_schema NOT IN ('information_schema', 'performance_schema', 'mysql', 'sys');
This query calculates the total data size of all databases on the MySQL server, including data and indexes. The total will appear on the first row, 总和
under the columns. This will help you understand the data usage on the entire MySQL server.
View the data size of a single MySQL database
If you want to see the data size of a specific database, you can use the following SQL query, replacing your_database_name
with the name of the database you want to see:
-- 显示单个数据库的数据大小
SELECT
table_schema AS `数据库`,
table_name AS `表名`,
CONCAT(ROUND(table_rows / 1000000, 2), 'M') AS `行数`,
CONCAT(ROUND(data_length / (1024 * 1024 * 1024), 2), 'GB') AS `数据大小`,
CONCAT(ROUND(index_length / (1024 * 1024 * 1024), 2), 'GB') AS `索引大小`,
CONCAT(ROUND((data_length + index_length) / (1024 * 1024 * 1024), 2), 'GB') AS `总大小`
FROM
information_schema.TABLES
WHERE
table_schema NOT IN ('information_schema', 'performance_schema', 'mysql', 'sys')
AND table_schema = 'your_database_name'
ORDER BY
data_length + index_length DESC;
This query will display the data size of each table in a specific database, including data and indexes. Just your_database_name
replace with the name of the database you want to view.
View all database data sizes
-- 显示所有数据库的总计
SELECT
'总和' AS `数据库`,
'' AS `表名`,
CONCAT(ROUND(SUM(table_rows) / 1000000, 2), 'M') AS `行数`,
CONCAT(ROUND(SUM(data_length) / (1024 * 1024 * 1024), 2), 'GB') AS `数据大小`,
CONCAT(ROUND(SUM(index_length) / (1024 * 1024 * 1024), 2), 'GB') AS `索引大小`,
CONCAT(ROUND((SUM(data_length) + SUM(index_length)) / (1024 * 1024 * 1024), 2), 'GB') AS `总大小`
FROM
information_schema.TABLES
WHERE
table_schema NOT IN ('information_schema', 'performance_schema', 'mysql', 'sys')
UNION
-- 显示每个表的数据大小
SELECT
table_schema AS `数据库`,
table_name AS `表名`,
CONCAT(ROUND(table_rows / 1000000, 2), 'M') AS `行数`,
CONCAT(ROUND(data_length / (1024 * 1024 * 1024), 2), 'GB') AS `表数据大小`,
CONCAT(ROUND(index_length / (1024 * 1024 * 1024), 2), 'GB') AS `索引大小`,
CONCAT(ROUND((data_length + index_length) / (1024 * 1024 * 1024), 2), 'GB') AS `表总大小`
FROM
information_schema.TABLES
WHERE
table_schema NOT IN ('information_schema', 'performance_schema', 'mysql', 'sys')
ORDER BY
`总大小` DESC; -- 修改ORDER BY子句
Summarize
By using the above SQL query, you can easily view the data size of your MySQL database and tables. This information is very helpful for database management, performance optimization and storage planning. Depending on your needs, you can run these queries periodically to monitor database growth and take appropriate action. Hope this blog is helpful!