Mysql

关注公众号 jb51net

关闭
首页 > 数据库 > Mysql > 统计mysql数据库占用磁盘空间大小和行数

统计mysql数据库占用磁盘空间大小和行数实例

作者:左边有只汪

本文介绍了如何查看MySQL数据库中每个表的占用磁盘空间大小和行数,通过执行特定的SQL查询语句,可以实现对数据库表的大小和行数的统计

统计mysql数据库占用磁盘空间大小和行数

1、查看占用磁盘空间大小

select sum(t1.data_size ) as data_sum_size,  
sum(t1.index_size) as index_sum_size
from(
select
TABLE_NAME,
table_schema,
truncate(data_length/1024/1024,2) as data_size, -- 查看数据占用大小单位为MB
truncate(index_length/1024/1024,2) as index_size -- 查看索引占用大小 单位为MB
from information_schema.tables
where TABLE_SCHEMA = '数据库名'
order by data_length desc) t1;

如果想查看该数据库中每个表占用大小的话

select
TABLE_NAME,
table_schema,
truncate(data_length/1024/1024,2) as data_size, -- 查看数据占用大小单位为MB
truncate(index_length/1024/1024,2) as index_size -- 查看索引占用大小 单位为MB
from information_schema.tables
where TABLE_SCHEMA = '数据库名'
order by data_length desc

2、查看数据库行数统计

select sum(t1.table_rows) as table_sum from (
select table_name,table_rows from information_schema.tables
where TABLE_SCHEMA = '数据库名'
order by table_rows desc) t1;

如果要查看数据库中每个表的行数的话

select table_name,table_rows from information_schema.tables
where TABLE_SCHEMA = '数据库名'
order by table_rows des

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文