Chinaunix首页 | 论坛 | 博客
  • 博客访问: 479746
  • 博文数量: 99
  • 博客积分: 3621
  • 博客等级: 中校
  • 技术积分: 1089
  • 用 户 组: 普通用户
  • 注册时间: 2010-06-22 16:29
文章存档

2012年(21)

2011年(28)

2010年(50)

分类: Mysql/postgreSQL

2010-11-15 15:37:06

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_options:
        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:创建一个表布局完全一样的新表,字段一样,不带索引;


 

阅读(1015) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~