查看: 657|回复: 0

MYSQL5.1---MYISAM表加快ALTER TABLE速度 之 【增加索引】

 关闭 [复制链接]

签到天数: 2623 天

连续签到: 2 天

[LV.Master]至尊海神IIIIII

发表于 2010-11-16 19:51 |广东| 显示全部楼层 |阅读模式

MYSQL里的ALTER TABLE一般情况下是通过先建立一个修改后结构的新表,然后把旧表的数据插入到新表,然后再把旧表删除掉。这样的方式下,对于超大表处理效率很差,修改大表等上几个小时很正常。下面测试一个“偷鸡”的办法,加速ALTER TABLE的速度。非官方办法,不保证一定成功,实验前请先备份好数据库。

1:看下表有多大

mysql> show table status from cpc like 'ter_test' \G
*************************** 1. row ***************************
           Name: ter_test
         Engine: MyISAM
        Version: 10
     Row_format: Fixed
           Rows: 30935313
Avg_row_length: 20
    Data_length: 618706260
Max_data_length: 5629499534213119
   Index_length: 317469696
      Data_free: 0
Auto_increment: 30935314
    Create_time: 2010-11-15 14:35:49
    Update_time: 2010-11-15 14:56:36
     Check_time: NULL
      Collation: utf8_general_ci
       Checksum: NULL
Create_opti**:
        Comment:
1 row in set (0.00 sec)

mysql>


3000多万行记录。再看看索引情况

mysql> show index from ter_test \G
*************************** 1. row ***************************
       Table: ter_test
  Non_unique: 0
    Key_name: PRIMARY
Seq_in_index: 1
Column_name: ter_test_id
   Collation: A
Cardinality: 30935313
    Sub_part: NULL
      Packed: NULL
        Null:
  Index_type: BTREE
     Comment:
1 row in set (0.00 sec)

mysql>


有一个主键索引。

试试添加一个普通索引看看。

mysql> alter table ter_test add index idx_1(mnc);
Query OK, 30935313 rows affected (5 min 41.48 sec)
Records: 30935313 Duplicates: 0 Warnings: 0

mysql>


居然跑了将近6分钟,我还以为死机了呢。把CPU全占死了。才3000万记录,如果有1E那不死翘。

来用“偷鸡”的办法

1:创建一个表布局完全一样的新表,字段一样,不带索引;



PCOS系统下载站:http://zhuangji.wang

本版积分规则