温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

mysql 表碎片整理

发布时间:2020-05-26 08:40:27 来源:网络 阅读:763 作者:春秋小记 栏目:MySQL数据库

查看数据库中表、索引和碎片大小的大小:
select round(sum(data_length/1024/1024),2) as data_length_MB,  
round(sum(index_length/1024/1024),2) as index_length_MB  ,
round(sum(data_free/1024/1024),2) as data_free_MB  ,table_name
from information_schema.tables where TABLE_SCHEMA= 'db_name' group by table_name order by 3 desc;更具查询的结果进行整理。

查看表的碎片情况:DATA_FREE
show TABLE status like 't_app_user';
或者查看:
select * from  information_schema.tables where table_schema= 't_app_user';
生成批量脚本:
select CONCAT('alter table ',table_name , ' ENGINE=INNODB;') from  information_schema.tables where TABLE_SCHEMA = 'db_chunqiu' and table_name like 't_app_user_head_%';


整理data_free大于100M的表:

select round(sum(data_length/1024/1024),2) as data_length_MB,  
round(sum(index_length/1024/1024),2) as index_length_MB  ,
round(sum(data_free/1024/1024),2) as data_free_MB  ,CONCAT('alter table ',table_name , ' ENGINE=INNODB;') dd
from information_schema.tables where TABLE_SCHEMA= 'db_chunqiu'  group by dd  having data_free_MB >100 order by 3 desc;

进行碎片整理:
alter table t_app_user ENGINE=INNODB;

整理前:
mysql> show TABLE status like 't_app_user'\G;
*************************** 1. row ***************************
           Name: st_order_cal_record
         Engine: InnoDB
        Version: 10
     Row_format: Dynamic
           Rows: 3033960
 Avg_row_length: 7117
    Data_length: 21594390528
Max_data_length: 0
   Index_length: 0
      Data_free: 201046622208 --200G碎片左右
 Auto_increment: 241541550
    Create_time: 2018-05-04 16:17:26
    Update_time: 2018-10-12 15:11:18
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options:
        Comment:
1 row in set (0.00 sec)

ERROR:
No query specified


整理后:
mysql> show TABLE status like 't_app_user'\G;
*************************** 1. row ***************************
           Name: st_order_cal_record
         Engine: InnoDB
        Version: 10
     Row_format: Dynamic
           Rows: 3292968
 Avg_row_length: 2038
    Data_length: 6711918592
Max_data_length: 0
   Index_length: 0
      Data_free: 4194304 --4M整理后
 Auto_increment: 241583900
    Create_time: 2018-10-12 15:14:30
    Update_time: 2018-10-12 15:57:51
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
 Create_options:
        Comment:
1 row in set (0.00 sec)

ERROR:
No query specified

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI