it编程 > 数据库 > Mysql

Mysql统计空间增量实现方式

84人参与 2026-05-10 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、字段含义

总结

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

(0)

您想发表意见!!点此发布评论

推荐阅读

统计mysql数据库占用磁盘空间大小和行数实例

05-10

MySQL事务&用户与权限管理方式

05-10

MySQL之JDBC编程用法及说明

05-10

MYSQL的慢SQL优化的实现

05-10

MySQL GPG密钥过期问题及解决方案

05-10

MySQL之存储过程与函数用法及说明

05-10

猜你喜欢

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

发表评论