• 【Mysql优化】索引碎片与维护


     

      在长期的数据更改过程中, 索引文件和数据文件,都将产生空洞,形成碎片.(不停的删除修改导致)

     

    解决办法:

    (1)我们可以通过一个nop操作(不产生对数据实质影响的操作), 来修改表.

      比如: 表的引擎为innodb , 可以 alter table xxx engine innodb(修改表的引擎类型为其默认类型会重新调整数据,但不会影响数据)

     

    mysql> SHOW create table tG
    *************************** 1. row ***********************
           Table: t
    Create Table: CREATE TABLE `t` (
      `id` int(11) NOT NULL AUTO_INCREMENT,
      `name` varchar(40) DEFAULT NULL,
      `idCard` char(18) DEFAULT NULL,
      `sex` char(1) DEFAULT NULL,
      PRIMARY KEY (`id`),
      UNIQUE KEY `idCard` (`idCard`),
      KEY `sex` (`sex`)
    ) ENGINE=MyISAM DEFAULT CHARSET=utf8
    1 row in set (0.02 sec)
    
    mysql> alter table t engine myisam;
    Query OK, 0 rows affected (0.25 sec)
    Records: 0  Duplicates: 0  Warnings: 0

     

     

     

     

    (2)optimize table 表名 ,也可以修复.

    mysql> optimize  table exam;
    +------------+----------+----------+--------------------------------------------
    -----------------------+
    | Table      | Op       | Msg_type | Msg_text
                           |
    +------------+----------+----------+--------------------------------------------
    -----------------------+
    | exam9.exam | optimize | note     | Table does not support optimize, doing recr
    eate + analyze instead |
    | exam9.exam | optimize | status   | OK
                           |
    +------------+----------+----------+--------------------------------------------
    -----------------------+
    2 rows in set (1.14 sec)

    注意: 修复表的数据及索引碎片,就会把所有的数据文件重新整理一遍,使之对齐.

        这个过程,如果表的行数比较大,也是非常耗费资源的操作.

        所以,不能频繁的修复.

     

    如果表的Update操作很频率,可以按周/月,来修复.

    如果不频繁,可以更长的周期来做修复.

  • 相关阅读:
    golang sync.WaitGroup
    golang 部分理解:关于channel 和 goroutine 例子
    golang filepath.Walk遍历指定目录下的所有文件
    golang filepath.Glob
    golang 函数传值
    golang panic and recover
    golang pipe
    golang 获取指定目录下的子文件列表
    eclipse:failed to create the java virtual machine
    如何在股市中捕捉涨停
  • 原文地址:https://www.cnblogs.com/qlqwjy/p/8594859.html
Copyright © 2020-2023  润新知