查看 MySQL「指定库」的容量大小

SELECT 
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024/1024, 2)) as '数据容量(GB)',
sum(truncate(index_length/1024/1024/1024, 2)) as '索引容量(GB)',
sum(truncate(DATA_FREE/1024/1024/1024, 2)) as '碎片占用(GB)'
from information_schema.tables
where table_schema='<数据库名>'
order by data_length desc, index_length desc;

查看 MySQL「指定库」中「所有表」的容量大小

SELECT
  table_schema as '数据库',
  table_name as '表名',
  table_rows as '记录数',
  truncate(data_length/1024/1024/1024, 2) as '数据容量(GB)',
  truncate(index_length/1024/1024/1024, 2) as '索引容量(GB)',
  truncate(DATA_FREE/1024/1024/1024, 2) as '碎片占用(GB)'
from 
  information_schema.tables
where 
  table_schema='<数据库名>'
order by 
  data_length desc, index_length desc;

查看表的大小,按碎片数排序

SELECT 
  TABLE_SCHEMA as '数据库',
  table_name as '表名',
  table_rows as '记录数',
  ENGINE as '存储引擎',
  truncate(data_length/1024/1024, 2) as '数据容量(MB)',
  truncate(index_length/1024/1024, 2) as '索引容量(MB)',
  truncate(DATA_FREE/1024/1024, 2) as '碎片占用(MB)'
from  information_schema.tables 
order by DATA_FREE desc limit 10;

查看 MySQL「所有库」的容量大小

SELECT 
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024/1024, 2)) as '数据容量(GB)',
sum(truncate(index_length/1024/1024/1024, 2)) as '索引容量(GB)',
sum(truncate(DATA_FREE/1024/1024/1024, 2)) as '碎片占用(GB)'
from information_schema.tables
group by table_schema
order by sum(data_length) desc, sum(index_length) desc;
Logo

DAMO开发者矩阵,由阿里巴巴达摩院和中国互联网协会联合发起,致力于探讨最前沿的技术趋势与应用成果,搭建高质量的交流与分享平台,推动技术创新与产业应用链接,围绕“人工智能与新型计算”构建开放共享的开发者生态。

更多推荐