教你如何统计Mysql数据库(表)的空间占用情况

有时候我们需要知道Mysql的空间占用情况,包括索引的大小等,此时我们可以通过以下语句进行统计

统计数据库:

SELECT 
  CONCAT(
    TRUNCATE(SUM(data_length) / 1024 / 1024, 2),
    'MB'
  ) AS data_size,
  CONCAT(
    TRUNCATE(SUM(max_data_length) / 1024 / 1024, 2),
    'MB'
  ) AS max_data_size,
  CONCAT(
    TRUNCATE(SUM(data_free) / 1024 / 1024, 2),
    'MB'
  ) AS data_free,
  CONCAT(
    TRUNCATE(SUM(index_length) / 1024 / 1024, 2),
    'MB'
  ) AS index_size 
FROM
  information_schema.tables 
WHERE TABLE_SCHEMA = '数据库名' ;

统计表:

SELECT 
  CONCAT(
    TRUNCATE(SUM(data_length) / 1024 / 1024, 2),
    'MB'
  ) AS data_size,
  CONCAT(
    TRUNCATE(SUM(max_data_length) / 1024 / 1024, 2),
    'MB'
  ) AS max_data_size,
  CONCAT(
    TRUNCATE(SUM(data_free) / 1024 / 1024, 2),
    'MB'
  ) AS data_free,
  CONCAT(
    TRUNCATE(SUM(index_length) / 1024 / 1024, 2),
    'MB'
  ) AS index_size 
FROM
  information_schema.tables 
WHERE TABLE_NAME = '表名' ;

 

Comments are closed, but trackbacks and pingbacks are open.