linux mysql数据库压缩表空间

查看表空间数据

select TABLE_SCHEMA, TABLE_NAME, `ENGINE`, TABLE_ROWS, AVG_ROW_LENGTH, DATA_LENGTH, INDEX_LENGTH, DATA_FREE, CREATE_TIME 
from information_schema.`TABLES` 
where DATA_FREE > 0  
and TABLE_SCHEMA = '逻辑库'
order by DATA_FREE desc;

-- 查看数据库中表大小及行数
select table_name,table_rows
,round((data_length / 1024 / 1024), 3) "data_length_MB" 
,round((index_length / 1024 / 1024), 3) "index_length_MB" 
,create_time,engine,table_collation 
from information_schema.tables
where table_schema = '逻辑库'
order by table_rows desc;

-- 碎片查询
select table_name,table_rows
,concat(round(DATA_FREE/1024/1024, 2), ' MB') as free_size
,concat(round(DATA_LENGTH/1024/1024, 2), ' MB') as size
,concat(DATA_FREE * 100/DATA_LENGTH,' %') as frag_percent
from information_schema.TABLES
where table_schema='逻辑库' and TABLE_TYPE='BASE TABLE'
order by DATA_LENGTH desc ;

压缩命令

show  databases;

use dev_log;

show create table wm_storage_his\G

show index from wm_storage_his;

optimize table bus_log; 

flush table bus_log;

如果不想把optimize table 写入从数据库,可以执行
OPTIMIZE NO_WRITE_TO_BINLOG TABLE 
这样命令就不会写入binglog 并且不被从库执行。


对于InnoDB表,OPTIMIZE TABLE被映射到ALTER TABLE上,这会重建表。重建操作能更新索引统计数据并释放成簇索引中的未使用的空间。

使用—skip-new或—safe-mode选项可以启动mysqld。通过启动mysqld,您可以使OPTIMIZE TABLE对其它表类型起作用。

注意,在OPTIMIZE TABLE运行过程中,MySQL会锁定表。

OPTIMIZE TABLE语句被写入到二进制日志中,除非使用了自选的NO_WRITE_TO_BINLOG关键词(或其别名LOCAL)。已经这么做了,因此,用于 MySQL服务器的OPTIMIZE TABLE命令的作用相当于一个复制主服务器,在默认情况下,这些命令将被复制到复制从属服务器中。
 

发布了11 篇原创文章 · 获赞 0 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/windforce828/article/details/84984116
今日推荐