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:表最后数据更新时间
相关推荐
程序员萌萌3 小时前
Java之mysql实战讲解(三):聚簇索引与非聚簇索引
java·mysql·聚簇索引
程序员萌萌4 小时前
Redis的缓存机制和淘汰策略详解
数据库·redis·缓存机制·淘汰策略
不剪发的Tony老师4 小时前
SQLite 3.53.0版本发布,重要更新
数据库·sqlite
Bczheng14 小时前
九.Berkeley DB数据库 序列化和钱包管理(1)
数据库
cozil5 小时前
记录mysql创建数据库未指定字符集引发的问题及解决方法
数据库·mysql
架构师老Y5 小时前
013、数据库性能优化:索引、查询与连接池
数据库·python·oracle·性能优化·架构
AC赳赳老秦5 小时前
OpenClaw数据库高效操作指南:MySQL/PostgreSQL批量处理与数据迁移实战
大数据·数据库·mysql·elasticsearch·postgresql·deepseek·openclaw
一 乐5 小时前
校园线上招聘|基于springboot + vue校园线上招聘系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·校园线上招聘系统
liliangcsdn5 小时前
如何基于sentence_transformers构建向量计算工具
数据库·人工智能·全文检索