当前位置: 代码网 > it编程>数据库>Mysql > MySQL如何查看某个表所占空间大小

MySQL如何查看某个表所占空间大小

2025年05月16日 Mysql 我要评论
一、使用sql查询查看表空间1.1 查询所有表的大小(包括数据和索引)select table_schema as '数据库名', table_name as '表名', roun

一、使用sql查询查看表空间

1.1 查询所有表的大小(包括数据和索引)

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 = '表名';

1.2 查询特定数据库的表大小

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;

1.3 查询单个表的详细空间信息

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 = '你的表名';

二、使用命令行工具查看表空间

2.1 使用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;"

2.2 查看物理文件大小(适用于myisam/innodb)

# 切换到mysql数据目录
cd /var/lib/mysql/你的数据库名/

# 查看文件大小
ls -lh *.ibd *.frm *.myd *.myi

# 计算总大小
du -sh ./*

三、查看innodb表的空间使用详情

3.1 查看innodb表空间状态

show table status from 你的数据库名 like '你的表名'\g

3.2 查看innodb引擎状态(包含缓冲池等信息)

show engine innodb status\g

3.3 查询innodb表空间文件信息

select 
    file_name, 
    tablespace_name, 
    engine, 
    total_extents, 
    extent_size, 
    initial_size, 
    maximum_size 
from 
    information_schema.files 
where 
    file_type = 'datafile';

四、高级空间分析工具

4.1 使用pt-diskstats(percona工具包)

pt-diskstats --devices=/var/lib/mysql

4.2 使用pt-mysql-summary(percona工具包)

pt-mysql-summary --user=用户名 --password=密码

4.3 使用mysqldumpslow分析表空间增长

mysqldumpslow -s t /var/log/mysql/mysql-slow.log

五、空间优化相关查询

5.1 查找碎片化严重的表

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;

5.2 查看表空间自动扩展设置

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数据库中各个表的空间占用情况,为数据库优化和维护提供数据支持。

到此这篇关于mysql如何查看某个表所占空间大小的文章就介绍到这了,更多相关mysql查看表空间大小内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com