基於MRG_MyISAM引擎的Mysql分表

  正常狀況下的分表,都是直接建立多個相同結構的表,好比table_一、table_2...最近碰到一個特殊需求,須要建立一個主表,全部分表的數據增刪改查,所有自動實時更新到主表,這個時候可使用MRG_MyISAM引擎了。sql

  首先建立主表`mygame_message_log`。而後建立分表(必須使用MyISAM引擎):spa

create table `mygame_message_log_1` LIKE `mygame_message_log`;
create table `mygame_message_log_2` LIKE `mygame_message_log`;
create table `mygame_message_log_3` LIKE `mygame_message_log`;
create table `mygame_message_log_4` LIKE `mygame_message_log`;
create table `mygame_message_log_5` LIKE `mygame_message_log`;
create table `mygame_message_log_6` LIKE `mygame_message_log`;
create table `mygame_message_log_7` LIKE `mygame_message_log`;
create table `mygame_message_log_8` LIKE `mygame_message_log`;
create table `mygame_message_log_9` LIKE `mygame_message_log`;
create table `mygame_message_log_10` LIKE `mygame_message_log`;
create table `mygame_message_log_11` LIKE `mygame_message_log`;
create table `mygame_message_log_12` LIKE `mygame_message_log`;
create table `mygame_message_log_13` LIKE `mygame_message_log`;
create table `mygame_message_log_14` LIKE `mygame_message_log`;
create table `mygame_message_log_15` LIKE `mygame_message_log`;
create table `mygame_message_log_16` LIKE `mygame_message_log`;
create table `mygame_message_log_17` LIKE `mygame_message_log`;
create table `mygame_message_log_18` LIKE `mygame_message_log`;
create table `mygame_message_log_19` LIKE `mygame_message_log`;
create table `mygame_message_log_20` LIKE `mygame_message_log`;

  

  而後將主表關聯到分表:設計

ALTER TABLE mygame_message_log 
    ENGINE = MERGE UNION=
        (mygame_message_log_1,
        mygame_message_log_2,
        mygame_message_log_3,
        mygame_message_log_4,
        mygame_message_log_5,
        mygame_message_log_6,
        mygame_message_log_7,
        mygame_message_log_8,
        mygame_message_log_9,
        mygame_message_log_10,
        mygame_message_log_11,
        mygame_message_log_12,
        mygame_message_log_13,
        mygame_message_log_14,
        mygame_message_log_15,
        mygame_message_log_16,
        mygame_message_log_17,
        mygame_message_log_18,
        mygame_message_log_19,
        mygame_message_log_20) 
    INSERT_METHOD=no AUTO_INCREMENT=1;
            

  

  須要注意的是,表的主鍵不能設爲自增,全部表的結構所有要一致。blog

  同時這種方法會有一個缺陷,就是一旦要修改表結構,就會很麻煩。千萬不能直接修改!table

  最好開始就將表設計考慮周到一點,若是實在要修改,可按以下步驟:class

  一、刪除主表;方法

  二、針對全部分表修改相應的字段;數據

  三、從新建立一個主表:tab

  

create table `mygame_message_log` LIKE `mygame_message_log_1`;

  四、給主表作關聯:di

ALTER TABLE mygame_message_log 
    ENGINE = MERGE UNION=
        (mygame_message_log_1,
        mygame_message_log_2,
        mygame_message_log_3,
        mygame_message_log_4,
        mygame_message_log_5,
        mygame_message_log_6,
        mygame_message_log_7,
        mygame_message_log_8,
        mygame_message_log_9,
        mygame_message_log_10,
        mygame_message_log_11,
        mygame_message_log_12,
        mygame_message_log_13,
        mygame_message_log_14,
        mygame_message_log_15,
        mygame_message_log_16,
        mygame_message_log_17,
        mygame_message_log_18,
        mygame_message_log_19,
        mygame_message_log_20) 
    INSERT_METHOD=no AUTO_INCREMENT=1;
            

  五、成功,原來主表的數據都恢復了

相關文章
相關標籤/搜索