本文介绍 MySQL 查看数据库表容量大小的命令语句

1. 查看所有数据库容量大小

  1. select table_schema as '数据库',
  2. sum(table_rows) as '记录数',
  3. sum(truncate(data_length / 1024 / 1024, 2)) as '数据容量(MB)',
  4. sum(truncate(index_length / 1024 / 1024, 2)) as '索引容量(MB)'
  5. from information_schema.tables
  6. group by table_schema
  7. order by sum(data_length) desc, sum(index_length) desc;

2. 查看所有数据库各表容量大小

  1. select table_schema as '数据库',
  2. table_name as '表名',
  3. table_rows as '记录数',
  4. truncate(data_length / 1024 / 1024, 2) as '数据容量(MB)',
  5. truncate(index_length / 1024 / 1024, 2) as '索引容量(MB)'
  6. from information_schema.tables
  7. order by data_length desc, index_length desc;

3. 查看指定数据库容量大小

例:查看 mysql 库容量大小

  1. select table_schema as '数据库',
  2. sum(table_rows) as '记录数',
  3. sum(truncate(data_length / 1024 / 1024, 2)) as '数据容量(MB)',
  4. sum(truncate(index_length / 1024 / 1024, 2)) as '索引容量(MB)'
  5. from information_schema.tables
  6. where table_schema = 'mysql';

4. 查看指定数据库各表容量大小

例:查看 mysql 库各表容量大小

  1. select table_schema as '数据库',
  2. table_name as '表名',
  3. table_rows as '记录数',
  4. truncate(data_length / 1024 / 1024, 2) as '数据容量(MB)',
  5. truncate(index_length / 1024 / 1024, 2) as '索引容量(MB)'
  6. from information_schema.tables
  7. where table_schema = 'mysql'
  8. order by data_length desc, index_length desc;