在MySQL数据库管理和优化中,了解表所占用的空间大小是非常重要的。以下是多种查看MySQL表空间大小的方法,包括SQL查询、命令行工具和可视化工具。
SELECT
table_schema AS '数据库名',
table_name AS '表名',
ROUND(data_length/1024/1024, 2) AS '数据大小(MB)',
ROUND(index_length/1024/1024, 2) AS '索引大小(MB)',
ROUND((data_length + index_length)/1024/1024, 2) AS '总大小(MB)',
table_rows AS '行数'
FROM
information_schema.TABLES
WHERE
table_schema NOT IN ('information_schema', 'mysql', 'performance_schema')
ORDER BY
(data_length + index_length) DESC;
SELECT round(data_length/1024/1024, 2) AS '数据大小(MB)', round(index_length/1024/1024, 2) AS '索引大小(MB)', round((data_length + index_length)/1024/1024, 2) AS '总大小(MB)'
FROM information_schema.TABLES
where table_name = '表名';
SELECT
table_name AS '表名',
ROUND(data_length/1024/1024, 2) AS '数据大小(MB)',
ROUND(index_length/1024/1024, 2) AS '索引大小(MB)',
ROUND((data_length + index_length)/1024/1024, 2) AS '总大小(MB)',
table_rows AS '行数'
FROM
information_schema.TABLES
WHERE
table_schema = '你的数据库名'
ORDER BY
(data_length + index_length) DESC;
SELECT
table_name AS '表名',
engine AS '存储引擎',
ROUND(data_length/1024/1024, 2) AS '数据大小(MB)',
ROUND(index_length/1024/1024, 2) AS '索引大小(MB)',
ROUND((data_length + index_length)/1024/1024, 2) AS '总大小(MB)',
ROUND(data_free/1024/1024, 2) AS '碎片空间(MB)',
table_rows AS '行数',
avg_row_length AS '平均行长度(字节)',
create_time AS '创建时间',
update_time AS '更新时间'
FROM
information_schema.TABLES
WHERE
table_schema = '你的数据库名'
AND table_name = '你的表名';
mysql
客户端查询mysql -u用户名 -p密码 -e "SELECT table_name AS '表名',
ROUND(data_length/1024/1024,2) AS ‘数据大小(MB)’,
ROUND(index_length/1024/1024,2) AS ‘索引大小(MB)’,
ROUND((data_length+index_length)/1024/1024,2) AS ‘总大小(MB)’
FROM information_schema.TABLES
WHERE table_schema=‘你的数据库名’
ORDER BY (data_length+index_length) DESC;"
# 切换到MySQL数据目录
cd /var/lib/mysql/你的数据库名/
# 查看文件大小
ls -lh *.ibd *.frm *.MYD *.MYI
# 计算总大小
du -sh ./*
SHOW TABLE STATUS FROM 你的数据库名 LIKE '你的表名'G
SHOW ENGINE INNODB STATUSG
SELECT
FILE_NAME,
TABLESPACE_NAME,
ENGINE,
TOTAL_EXTENTS,
EXTENT_SIZE,
INITIAL_SIZE,
MAXIMUM_SIZE
FROM
INFORMATION_SCHEMA.FILES
WHERE
FILE_TYPE = 'DATAFILE';
pt-diskstats
(Percona工具包)pt-diskstats --devices=/var/lib/mysql
pt-mysql-summary
(Percona工具包)pt-mysql-summary --user=用户名 --password=密码
mysqldumpslow
分析表空间增长mysqldumpslow -s t /var/log/mysql/mysql-slow.log
SELECT
table_schema AS '数据库',
table_name AS '表名',
ROUND(data_free/1024/1024, 2) AS '碎片空间(MB)',
ROUND((data_length + index_length)/1024/1024, 2) AS '总大小(MB)',
ROUND((data_free/(data_length + index_length + data_free))*100, 2) AS '碎片率(%)'
FROM
information_schema.TABLES
WHERE
table_schema NOT IN ('information_schema', 'mysql', 'performance_schema')
AND data_free > 0
ORDER BY
data_free DESC
LIMIT 10;
SELECT
table_name,
engine,
row_format,
create_options
FROM
information_schema.TABLES
WHERE
table_schema = '你的数据库名';
权限要求:查询information_schema
需要相应的权限
数据准确性:table_rows
是估算值,特别是对于InnoDB表
存储引擎差异:
InnoDB表数据存储在.ibd
文件中(独立表空间)或共享表空间中
MyISAM表数据存储在.MYD
文件中,索引存储在.MYI
文件中
临时表空间:临时表和使用内存引擎的表不会显示在磁盘使用统计中
二进制日志和事务日志:这些日志文件占用空间但不包含在表空间统计中
#!/bin/bash
# MySQL表空间监控脚本
DB_USER="用户名"
DB_PASS="密码"
DB_NAME="数据库名"
OUTPUT_FILE="/tmp/mysql_table_sizes_$(date +%Y%m%d).csv"
echo "表名,数据大小(MB),索引大小(MB),总大小(MB),行数,碎片空间(MB)" > $OUTPUT_FILE
mysql -u$DB_USER -p$DB_PASS -e "SELECT
CONCAT(table_name, ',',
ROUND(data_length/1024/1024, 2), ',',
ROUND(index_length/1024/1024, 2), ',',
ROUND((data_length + index_length)/1024/1024, 2), ',',
table_rows, ',',
ROUND(data_free/1024/1024, 2))
FROM information_schema.TABLES
WHERE table_schema = ‘$DB_NAME’
ORDER BY (data_length + index_length) DESC;" >> $OUTPUT_FILE
echo "报告已生成: $OUTPUT_FILE"
通过以上方法,您可以全面了解MySQL数据库中各个表的空间占用情况,为数据库优化和维护提供数据支持。