本文整理自网络,侵删。
目录
- 场景:
- 官网的解释:
- 补充:查询表大小
- 总结
场景:
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%,所以只是一个大概的统计
相关阅读 >>
更多相关阅读请进入《sql》频道 >>
数据库系统概念 第6版
机械工业出版社
本书主要讲述了数据模型、基于对象的数据库和XML、数据存储和查询、事务管理、体系结构等方面的内容。
转载请注明出处:木庄网络博客 » MySQL如何统计一个数据库所有表的数据量
标签:sql
相关推荐
评论
管理员已关闭评论功能...