MySQL如何统计一个数据库所有表的数据量
作者:smileNicky 时间:2024-01-23 20:07:14
场景:
mysql统计一个数据库里所有表的数据量,最近在做统计想查找一个数据库里基本所有的表数据量,数据量少的通过select count再加起来也是可以的,不过表的数据有点多,不可能一个一个地查
记得在Navicat里,选择一个数据量,点击表,如图:
是可以看到所有表具体的数据行的
然后可以通过sql实现?在mysql里是可以查询information_schema.tables这张表的
SELECT table_rows,table_name FROM information_schema.tables
WHERE TABLE_SCHEMA = '数据库名称'
and table_name not in ('不查询的表名称')
ORDER BY table_rows DESC;
要统计的,加上sum函数就可以
SELECT sum(table_rows) FROM information_schema.tables
WHERE TABLE_SCHEMA = '数据库名称'
and table_name not in ('不查询的表名称')
ORDER BY table_rows DESC;
OK,本来还以为已经没问题了,然后还是被反馈统计不对,后来去找了资料
https://dev.mysql.com/doc/refman/8.0/en/information-schema-tables-table.html
官网的解释:
TABLE_ROWS
The number of rows. Some storage engines, such as MyISAM, store the exact count. For other storage engines, such as InnoDB, this value is an approximation, and may vary from the actual value by as much as 40% to 50%. In such cases, use SELECT COUNT(*) to obtain an accurate count.
TABLE_ROWS is NULL for INFORMATION_SCHEMA tables.
For InnoDB tables, the row count is only a rough estimate used in SQL optimization. (This is also true if the InnoDB table is partitioned.)
大概意思是对于MyISAM才是正确的统计数据,但是对于InnoDB引擎的,可能与实际值相差 40% 到 50%,所以只是一个大概的统计
所以针对这种情况,要更改存储引擎,肯定是不太合适,因为InnoDB是默认的存储引擎,能支持事务外健,并 * 况性能也比较好
所以,根据网上的做法,重新analyze 对应表,在mysql8.0版本是不管用的,发现查询数据还是不对,估计是mysql版本太高,mysql5版本没验证过
analyze table [table_name]
继续找资料,在Navicat工具->命令行页面,设置全局或者回话的information_schema_stats_expiry为0,表示自动更新,设置全局的不知道会不会影响性能,所以不知道还是设置会话的就可以
SET SESSION information_schema_stats_expiry=0;
SET @@SESSION.information_schema_stats_expiry=0;
查询设置的information_schema_stats_expiry值
show variables like '%information_schema_stats%';
MySQL 8.0为了提高information_schema的查询效率,会将视图tables和statistics里面的统计信息缓存起来,缓存过期时间由参数information_schema_stats_expiry决定
补充:查询表大小
我需要查询的库名为:kite
因此sql语句为:
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='kite'
order by table_rows desc, index_length desc;
结果如下:
总结
来源:https://blog.csdn.net/u014427391/article/details/121903989
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
详解Go 中的时间处理
擦亮自己的眼睛去看SQL Server之历史渊源
node实现socket链接与GPRS进行通信的方法
![](https://img.aspxhome.com/file/2023/5/125735_0s.jpg)
学习Python,你还不知道main函数吗
![](https://img.aspxhome.com/file/2023/3/131063_0s.png)
MySQL中数据查询语句整理大全
利用matlab与Excel交互之单元格操作
![](https://img.aspxhome.com/file/2023/4/118764_0s.png)
Python中对字典的几个处理方法分享
简单的抓取淘宝图片的Python爬虫
趣味Python实战练习之自动更换桌面壁纸脚本附源码
![](https://img.aspxhome.com/file/2023/9/82549_0s.jpg)
常见数据库系统比较 Oracle数据库
最长用最基本的MSSQL数据库备份与还原
Python3.6日志Logging模块简单用法示例
![](https://img.aspxhome.com/file/2023/1/135241_0s.png)
基于Python实现2种反转链表方法代码实例
![](https://img.aspxhome.com/file/2023/3/80783_0s.jpg)
关于scipy.optimize函数使用及说明
基于python实现判断字符串是否数字算法
![](https://img.aspxhome.com/file/2023/1/83831_0s.png)
Python程序中使用SQLAlchemy时出现乱码的解决方案
asp+ajax版四级联动菜单(数据库)
numpy.std() 计算矩阵标准差的方法
Pyinstaller+Pipenv打包Python文件的实现示例
![](https://img.aspxhome.com/file/2023/4/82594_0s.png)