侧边栏壁纸
博主头像
大凯同学博主等级

又一个春夏秋冬开始了,我还是我

  • 累计撰写 35 篇文章
  • 累计创建 59 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

数据库优化常用SQL

大凯同学
2022-10-07 / 0 评论 / 0 点赞 / 41 阅读 / 410 字

在工作中会经常对数据库进行运维操作,例如大表清理等等.以下记录几个常用的SQL语句,以备不时之需。

统计同一个实例下面的所有数据库的容量大小

SELECT 
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)',
sum(truncate(DATA_FREE/1024/1024, 2)) as '碎片占用(MB)'
from information_schema.tables
group by table_schema
order by sum(data_length) desc, sum(index_length) desc;

统计指定数据库的占用空间

SELECT 
table_schema as '数据库',
sum(table_rows) as '记录数',
sum(truncate(data_length/1024/1024, 2)) as '数据容量(MB)',
sum(truncate(index_length/1024/1024, 2)) as '索引容量(MB)',
sum(truncate(DATA_FREE/1024/1024, 2)) as '碎片占用(MB)'
from information_schema.tables
where table_schema='xxx';

统计数据库中表占用的空间

统计同一个实例下面的所有表的容量大小

select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
order by data_length desc, index_length desc;

统计指定数据库各表的容量大小

select
table_schema as '数据库',
table_name as '表名',
table_rows as '记录数',
truncate(data_length/1024/1024, 2) as '数据容量(MB)',
truncate(index_length/1024/1024, 2) as '索引容量(MB)'
from information_schema.tables
where table_schema='xxx'
order by data_length desc, index_length desc;
0

评论区