Mysql统计空间增量

1、统计【所有库】每月新增空间总量

复制代码
SELECT
    DATE_FORMAT(create_time, '%Y-%m') AS 月份,
    ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS 新增总空间_MB,
    ROUND(SUM(data_length + index_length) / 1024 / 1024 / 1024, 2) AS 新增总空间_GB
FROM
    information_schema.tables
WHERE
    table_schema NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys') -- 排除系统库
    AND create_time IS NOT NULL -- 只统计有创建时间的表
GROUP BY
    DATE_FORMAT(create_time, '%Y-%m')
ORDER BY
    月份 DESC;

2、统计【指定库】每月新增空间

your_database 换成你的数据库名即可:

复制代码
SELECT
    DATE_FORMAT(create_time, '%Y-%m') AS 月份,
    ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS 新增空间_MB,
    ROUND(SUM(data_length + index_length) / 1024 / 1024 / 1024, 2) AS 新增空间_GB
FROM
    information_schema.tables
WHERE
    table_schema = 'your_database' -- 替换成你的库名
    AND create_time IS NOT NULL
GROUP BY
    DATE_FORMAT(create_time, '%Y-%m')
ORDER BY
    月份 DESC;

3、更精准:按【修改时间】统计每月增量(推荐)

create_time 是表创建时间,实际业务中更应该用 update_time(表最后修改时间) 代表每月数据增长:

复制代码
SELECT
    DATE_FORMAT(update_time, '%Y-%m') AS 月份,
    ROUND(SUM(data_length + index_length) / 1024 / 1024, 2) AS 当月总占用_MB,
    ROUND(SUM(data_length + index_length) / 1024 / 1024 / 1024, 2) AS 当月总占用_GB
FROM
    information_schema.tables
WHERE
    table_schema NOT IN ('information_schema', 'mysql', 'performance_schema', 'sys')
    AND update_time IS NOT NULL
GROUP BY
    DATE_FORMAT(update_time, '%Y-%m')
ORDER BY
    月份 DESC;

4、字段含义

  • data_length:数据文件大小
  • index_length:索引文件大小
  • data_length + index_length = 表总空间
  • create_time:表创建时间
  • update_time:表最后数据更新时间
相关推荐
unDl IONA几秒前
mysql之如何获知版本
数据库·mysql
俺不要写代码11 分钟前
数据库:约束
数据库·mysql
KmSH8umpK18 分钟前
Redis分布式锁从原生手写到Redisson高阶落地,附线上死锁复盘优化方案进阶第四篇
数据库·redis·分布式
KmSH8umpK1 小时前
Redis分布式锁从原生手写到Redisson高阶落地,附线上死锁复盘优化方案进阶第五篇
数据库·redis·分布式
lilihuigz1 小时前
企业培训网站搭建指南:5步在WordPress上创建品牌学院
数据库
WL_Aurora1 小时前
MySQL 5 卸载到 MySQL 8 安装完整指南(不踩坑版)
数据库·mysql
灰阳阳1 小时前
MySQL的基本架构
数据库·mysql·架构
@小柯555m1 小时前
MySql(高级操作符--Where in 和Not in)
数据库·sql·mysql
许彰午1 小时前
CacheSQL(一):手写数据库的工程化重生
java·数据库·缓存
MmeD UCIZ1 小时前
MySQL单表存多大的数据量比较合适
数据库·mysql