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:表最后数据更新时间
相关推荐
2501_9318037516 小时前
MySQL 增删改查详解
数据库
Sagittarius_A*16 小时前
【LitCTF2026】lit_ezsql
android·java·数据库
ttwuai17 小时前
Go 后台初始化 MySQL 脚本失败怎么办?先查 DDL 还是生成配置
开发语言·mysql·golang
未秃头的程序猿17 小时前
分库分表一年后,我复盘了当时最该想清楚的三件事
java·数据库·后端
心易行者17 小时前
用html在线运行做数据可视化大屏,5个实战场景从入门到上线
大数据·前端·数据库·人工智能·python
꯭自꯭闭꯭17 小时前
达梦(DM8)安装测试
linux·运维·服务器·数据库
袁因18 小时前
Oracle 慢 SQL 优化:Cost 最高的那行不是瓶颈,真凶藏在 SELECT 列表里
数据库
天衍四九-18 小时前
Agent Skills从入门到工程化(十六):面试中如何讲清楚 Agent Skills?
大数据·数据库·人工智能·python·chatgpt·面试
H_oRIZoN_18 小时前
Linux入门DAY37(IO 多路复用、TCP 并发服务器与数据库 SQLite3 详解)
linux·服务器·数据库