生产的数据库有很多大表,数据量非常大已经对系统稳定产生影响。经过沟通,这些表中之前存放每天的数据,改为只存放每个月最后一天的数据。

下面记录一些常用的查询大表操作语句

查看所有数据库的大小

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

查看所有表的大小并排序

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

找到一些非常大的表,对该表进行操作

数据库表名记录数数据容量(GB)索引容量(GB)
pccmdbcaf_tbyh_daily864560564404.20360.42
pccmdbcaf_tbyh_jbdaily207974565259.2090.78
pccmdbcaf_tbyh_jbcxqd710513371253.26237.65
pccmdbrg_to_crsp_yj7321262353.8042.18

如果有的大表师分区表,删除表分区的语句如下:

ALTER TABLE tablename DROP partition partitionname

勿念
1 声望0 粉丝