现在的位置: 首页 > 综合 > 正文

生产环境 MySQL 表的维护:check、optimize和analyze

2013年08月31日 ⁄ 综合 ⁄ 共 2025字 ⁄ 字号 评论关闭
     ㈠ optimize
        
        optimize可以回收空间、减少碎片、提高I/O
        目前支持的存储引擎有:InnoDB、MyASIM和ARCHIVE
        
        如果是Replication环境、可加NO_WRITE_TO_BINLOG(或者LOCAL、意思完全相同)、比如:
        optimize local table table_name;
        

        以下是一个简单测试:

[mysql@odd employees]$ ls -alh t.ibd
-rw-rw---- 1 mysql dba 24M 05-22 16:48 t.ibd

未optimize前、有24M

mysql> optimize table t;
+-------------+----------+----------+-------------------------------------------------------------------+
| Table       | Op       | Msg_type | Msg_text                                                          |
+-------------+----------+----------+-------------------------------------------------------------------+
| employees.t | optimize | note     | Table does not support optimize, doing recreate + analyze instead |
| employees.t | optimize | status   | OK                                                                |
+-------------+----------+----------+-------------------------------------------------------------------+
2 rows in set (3.82 sec)

--对于InnoDB的表、上面的内容并非报错、这是MySQL会帮你映射到:alter table table_name engine='InnoDB';
--MyISAM不会有这种情况


[mysql@odd employees]$ ls -alh t.ibd
-rw-rw---- 1 mysql dba 14M 05-22 16:49 t.ibd
  
optimize后、剩14M

     ㈡ check
        
        检查表或视图的有无错误
        支持表引擎有:InnoDB和MyISAM
        
        下面简单模拟一个测试:

mysql> check table t;
+-------------+-------+----------+----------+
| Table       | Op    | Msg_type | Msg_text |
+-------------+-------+----------+----------+
| employees.t | check | status   | OK       |
+-------------+-------+----------+----------+
1 row in set (0.63 sec)

--没有错误的情况是这样的

--用vim打开t.frm随意编辑两把

mysql> check table t\G;
*************************** 1. row ***************************
   Table: employees.t
      Op: check
Msg_type: Error
Msg_text: Incorrect information in file: './employees/t.frm'
*************************** 2. row ***************************
   Table: employees.t
      Op: check
Msg_type: error
Msg_text: Corrupt
2 rows in set (0.00 sec)

--报错了

     
     ㈢ analyze
        
        用于收集优化器统计信息、和tuning相关、
        这个命令对 MyISAM、BDB、InnoDB 存储引擎的表有作用
        如果不想记录到binlog、也可加关键字local或者另外一个

mysql> analyze table t\G;
*************************** 1. row ***************************
   Table: employees.t
      Op: analyze
Msg_type: Error
Msg_text: Incorrect information in file: './employees/t.frm'
*************************** 2. row ***************************
   Table: employees.t
      Op: analyze
Msg_type: error
Msg_text: Corrupt
2 rows in set (0.00 sec)

【上篇】
【下篇】

抱歉!评论已关闭.