【MySQL】查看库与表的占用空间

发布时间 2023-04-17 14:18:35作者: Syw_文
# 查看各个库占用空间
SELECT
    TABLE_SCHEMA,
    concat( TRUNCATE ( sum( data_length )/ 1024 / 1024, 2 ), ' MB' ) AS data_size,
    concat( TRUNCATE ( sum( index_length )/ 1024 / 1024, 2 ), 'MB' ) AS index_size 
FROM
    information_schema.TABLES 
GROUP BY
    TABLE_SCHEMA 
ORDER BY
    data_length DESC;
  
# 查看某个表占用空间
SELECT
    concat( round( sum( data_length / 1024 / 1024 ), 2 ), 'MB' ) AS data_length_MB,
    concat( round( sum( index_length / 1024 / 1024 ), 2 ), 'MB' ) AS index_length_MB 
FROM
    information_schema.TABLES 
WHERE
    table_schema = 'test' 
    AND table_name = 'test_tb'

其他的补充信息如下

# 查看某个库中的表信息
SELECT
    table_name,
    table_type,
  ENGINE 
FROM
    information_schema.TABLES 
WHERE
    table_schema = 'db5' 
ORDER BY
    table_name

# 查看整个实例占用空间
SELECT
    concat( round( sum( data_length / 1024 / 1024 ), 2 ), 'MB' ) AS data_length_MB,
    concat( round( sum( index_length / 1024 / 1024 ), 2 ), 'MB' ) AS index_length_MB 
FROM
    information_schema.TABLES;