Mysql 查询数据库容量大小的方法步骤

作者:Jevic 时间:2024-01-20 13:56:35 

查询所有数据库的总大小

方法如下:


mysql> use information_schema;
mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from TABLES;
+-----------+
| data   |
+-----------+
| 3052.76MB |
+-----------+
1 row in set (0.02 sec)

统计一下所有库数据量

每张表数据量=AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH


SELECT
SUM(AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH)/1024/1024 AS total_mb
FROM information_schema.TABLES

统计每个库大小:


SELECT
table_schema,SUM(AVG_ROW_LENGTH*TABLE_ROWS+INDEX_LENGTH)/1024/1024 AS total_mb
FROM information_schema.TABLES group by table_schema;

第二种情况:查看指定数据库的大小,比如说:数据库test,方法如下:


mysql> use information_schema;
mysql> select concat(round(sum(DATA_LENGTH/1024/1024),2),'MB') as data from TABLES where table_schema='test';
+----------+
| data   |
+----------+
| 142.84MB |
+----------+
1 row in set (0.00 sec)

1.查看所有数据库各容量大小


select
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'
from information_schema.tables
group by table_schema
order by sum(data_length) desc, sum(index_length) desc;

2.查看所有数据库各表容量大小


select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
order by data_length desc, index_length desc;

3.查看指定数据库容量大小


例:查看mysql库容量大小
select
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)'
from information_schema.tables
where table_schema='mysql';

4.查看指定数据库各表容量大小


例:查看mysql库各表容量大小

select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
where table_schema='mysql'
order by data_length desc, index_length desc;

题外方法

直接用shell命令统计mysql data目录中的大小(注意只有库,不包含数据库日志大小)

备注 :

data_length :存储数据大小

data_length/1024/1024:将字节转换为MB

round(sum(data_length/1024/1024),2):取两位小数

concat(round(sum(data_length/1024/1024),2),'MB') :给计算结果追加单位 “MB”

来源:https://segmentfault.com/a/1190000022874702

标签:Mysql,查询,数据库,容量
0
投稿

猜你喜欢

  • Python实现对excel文件列表值进行统计的方法

    2022-08-08 17:42:23
  • python写入文件自动换行问题的方法

    2022-06-13 11:28:18
  • 使用shell检查并修复mysql数据库表的脚本

    2024-01-27 23:52:35
  • 学习ASP.NET八天入门:第二天

    2007-08-07 13:24:00
  • Python内置函数详细解析

    2021-08-19 21:03:35
  • Python Pytorch深度学习之Tensors张量

    2023-05-04 12:34:54
  • 解决mysql ERROR 1017:Can't find file: '/xxx.frm' 错误

    2024-01-13 18:57:43
  • python学习必备知识汇总

    2022-01-05 10:26:23
  • jquery表单验证使用插件formValidator

    2023-07-02 05:30:54
  • 编写高质量代码的30条黄金守则(首选隐式类型转换)

    2022-07-19 13:03:41
  • 5个有效改进网页UI设计的技巧

    2008-12-19 12:04:00
  • 用Python实现web端用户登录和注册功能的教程

    2021-03-03 07:49:09
  • Python探索之ModelForm代码详解

    2022-05-16 06:14:33
  • ASP获取网址或当前地址代码

    2008-04-07 20:19:00
  • 测试、预发布后用python检测网页是否有日常链接

    2023-03-31 20:12:44
  • Go语言基础数组用法及示例详解

    2024-04-26 17:33:52
  • Python tkinter的grid布局及Text动态显示方法

    2023-06-28 14:54:00
  • python中__slots__用法实例

    2022-05-29 17:49:22
  • 如何利用python查找电脑文件

    2022-02-16 18:30:37
  • Django 限制访问频率的思路详解

    2021-08-17 16:52:57
  • asp之家 网络编程 m.aspxhome.com