mysql查看库、表大小
封面图
•
2025-10-11
•
MySQL
197
0
0
0
在 MySQL 中,可以通过查询系统表(如 `information_schema`)来查看数据库、表的大小。以下是一些常用的 SQL 查询语句: --- ## 一、查看所有数据库的大小 ```sql SELECT table_schema AS 'Database', SUM(data_length + index_length) / 1024 / 1024 AS 'Size (MB)' FROM information_schema.tables GROUP BY table_schema; ``` --- ## 二、查看某个数据库中所有表的大小 ```sql SELECT table_name AS 'Table', SUM(data_length + index_length) / 1024 / 1024 AS 'Size (MB)' FROM information_schema.tables WHERE table_schema = 'your_database_name' GROUP BY table_name; ``` --- ## 三、查看某个表的详细信息(包括数据和索引大小) ```sql SELECT table_name AS 'Table', data_length / 1024 / 1024 AS 'Data Size (MB)', index_length / 1024 / 1024 AS 'Index Size (MB)', (data_length + index_length) / 1024 / 1024 AS 'Total Size (MB)' FROM information_schema.tables WHERE table_schema = 'your_database_name' AND table_name = 'your_table_name'; ``` --- ## 四、查看每个表的行数(可选) ```sql SELECT table_name AS 'Table', table_rows AS 'Rows' FROM information_schema.tables WHERE table_schema = 'your_database_name'; ``` > 注意:`table_rows` 是估算值,不是精确值。 --- ## 五、查看数据库的总大小(更精确的方式) 如果你使用的是 **InnoDB** 存储引擎,还可以通过查看 `.ibd` 文件的大小来获取更准确的表空间占用情况,但这通常需要访问文件系统。 --- ## 六、使用命令行工具(MySQL 客户端) 你也可以在 MySQL 命令行中直接运行这些查询,例如: ```bash mysql -u root -p -e "SELECT table_schema, SUM(data_length + index_length)/1024/1024 AS 'Size (MB)' FROM information_schema.tables GROUP BY table_schema;" ``` --- ## 小贴士: - 如果你的 MySQL 版本较旧,可能不支持某些字段(如 `index_length`),可以查阅官方文档确认。 - 如果使用的是 **MariaDB**,方法类似,但部分字段可能略有不同。 --- 如果你告诉我具体是哪个数据库或表,我可以帮你生成更具体的查询语句。
上一篇:
mySQL查询优化
下一篇:
查看 MySQL 数据库中每个表占用的空间大小
标题录入,一次不能超过6条
返回顶部
留言
留言
评论