1.ANALYZE TABLE
分析的作用是查看表的关键字分布,能够让 sql 生成正确的执行计划。
--分析时不写binlog;
mysql> analyze no_write_to_binlog table test1; 一般分析不写日志。
mysql> analyze local table test1;
2.CHECK TABLE : 检查表的完整性
检查表结构
用来检查数据库表和索引是否损坏。
如果mysql 查询一个表后打印Incorrect key file for table '***'; try to repair it,
则说明这张表坏了。
CHECK TABLE test1;
3.OPTIMIZE TABLE : 优化表
optimize 可以回收空间、减少碎片、提高I/O
目前支持的存储引擎有:InnoDB、MyASIM 和ARCHIVE
支持分区表
OPTIMIZE TABLE 选项:
NO_WRITE_TO_BINLOG 或LOCAL:不记录二进制日志
mysql> OPTIMIZE no_write_to_binlog table test1; 一般分析不写日志。
mysql> OPTIMIZE local table test1;
MySQL官方建议不要经常(每小时或每天)进行碎片整理,
一般根据实际情况,只需要每周或者每月整理一次
OPTIMIZE TABLE只对MyISAM,BDB和InnoDB表起作用,尤其是MyISAM表的作用最为明显。
然而,并不是所有表都需要进行碎片整理,一般只需要对包含变长的文本数据类型(varchar)的表进行碎片整理。
在OPTIMIZE TABLE运行过程中,MySQL会锁定表。
默认情况下,直接对InnoDB引擎的数据表使用OPTIMIZE TABLE,可能会显示
「 Table does not support optimize, doing recreate + analyze instead」的提示信息。
需要在mysqld启动mysql的时候加上--skip-new 或 --safe-mode。
对于innodb存储引擎的表:OPTIMIZE no_write_to_binlog table test1;
--相当于下面的语句。
alter table test1 engine=innodb;
4.REPAIR TABLE : 修复表
修复可能已经损坏的MyISAM 或ARCHIVE 表
--不支持InnoDB
优化过程中会锁定表,支持分区表
REPAIR TABLE 选项:
QUICK : 仅仅修复索引树
EXTENDED : 逐行创建索引(替代以排序方式一次创建一个索引)
USE_FRM : 使用.FRM 文件重新创建.MYI 文件
NO_WRITE_TO_BINLOG 或LOCAL:不记录二进制日志
check table 语句可以检查一个表中的的问题,若表或索引损坏,
可以使用repair table 语句尝试修正它。
如果不起作用,可以使用myisamchk 这样的工具。
mysql> repair local table test1;
mysql> repair local table test1 quick;
mysql> repair local table test1 extended;
mysql> repair local table test1 use_frm;
mysql> repair no_write_to_binlog table test1 use_frm;
5. mysqlcheck 检查表是否损坏
[root@mysql1 test]# mysqlcheck -c test -uroot -prootroot
mysqlcheck: [Warning] Using a password on the command line interface can be insecure.
test.deadlocks OK
test.heartbeat OK
test.t OK
test.t2 OK
test.test1 OK
test.test_pt OK
6.mysqlcheck分析test数据库所有表
[root@mysql1 test]# mysqlcheck -a test -uroot -prootroot
mysqlcheck: [Warning] Using a password on the command line interface can be insecure.
test.deadlocks OK
test.heartbeat OK
test.t OK
test.t2 OK
test.test1 OK
test.test_pt
OK
7.mysqlcheck优化test数据库里面所有表
mysqlcheck -o test -uroot -prootroot
mysqlcheck -o test test1 -uroot -prootroot
8.总结
analyze table test;
check table test;
optimize table test;
repaire table test;
分析,检查,优化,修复 四个步骤非常方便的维护mysql数据库的表损坏。
文章介绍了MySQL中用于维护表的四个关键命令:ANALYZETABLE用于查看关键字分布和优化执行计划;CHECKTABLE用于检查表的完整性和损坏情况;OPTIMIZETABLE用于回收空间和减少碎片;REPAIRTABLE则用于修复可能损坏的表。这些操作对于数据库性能和稳定性至关重要,但过度使用可能影响性能,应根据实际需求适当执行。
1346

被折叠的 条评论
为什么被折叠?



