MySQL统计一个数据库中所有表的数据量

mysql统计一个数据库里所有表的数据量,最近在做统计想查找一个数据库里基本所有的表数据量,数据量少的通过select count再加起来也是可以的,不过表的数据有点多,不可能一个一个地查

在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;

不过统计出来的可能会有误差

官网的解释: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是默认的存储引擎,能支持事务外健,并发情况性能也比较好

posted @ 2023-01-09 16:21  玩转大数据  阅读(421)  评论(0编辑  收藏  举报