Chinaunix首页 | 论坛 | 博客
  • 博客访问: 128831
  • 博文数量: 89
  • 博客积分: 2580
  • 博客等级: 少校
  • 技术积分: 775
  • 用 户 组: 普通用户
  • 注册时间: 2009-01-05 20:09
文章分类

全部博文(89)

文章存档

2009年(89)

我的朋友

分类: Mysql/postgreSQL

2009-08-17 11:23:35


首先,分析以下alter一个超大表低效率的原因:
1. mysql中alter表的机制是:创建一个符合alter目标的表,然后把数据全部插入到新表中,然后删除老表。
2. 在数据迁移时,每插入一行,需要对索引进行一次更新。效率低下且会产生索引碎片
3. 当没有足够的内存,或者表上的索引非常多时,效率十分的低下。

避免方式:

1. 使用正确的alter语句
   如露alter的目标是更改某列的default值。mysql有特定的语句只对frm文件进行操作,而不更新index和data。
   错误的方式:alter table tbl_name modify column col1 int not null default 5;
   正确的方式:alter table tbl_name alter column col1 set default 5;

2. 手动的执行alter的机制
   首先,create tbl_new like tbl_old;
   然后,alter table modify column ...
   然后,alter tbl_new disable keys;
   ---load data---
   然后,alter tbl_new enable keys;
   最后,rename table tbl_old to tbl_new,tbl_new to tbl_old;

3. 人工修改frm表(危险!!!)
   在5.1.36的myisam下,对这种方法进行了测试,并没有成功,会报index corrupted的错误。

   首先,create tbl_new like tbl_old;
   然后,alter table modify column ...
   然后,flush tables with read lock;
   接着在shell中将两个表的frm文件互换。
   最后,unlock tables;



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