当前位置: 代码网 > it编程>数据库>Mysql > Mysql统计空间增量实现方式

Mysql统计空间增量实现方式

2026年05月10日 Mysql 我要评论
1、统计【所有库】每月新增空间总量select date_format(create_time, '%y-%m') as 月份, round(sum(data_length + index

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:表最后数据更新时间

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持代码网。

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2026  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com