Chinaunix首页 | 论坛 | 博客
  • 博客访问: 26314503
  • 博文数量: 2065
  • 博客积分: 10377
  • 博客等级: 上将
  • 技术积分: 21525
  • 用 户 组: 普通用户
  • 注册时间: 2008-11-04 17:50
文章分类

全部博文(2065)

文章存档

2012年(2)

2011年(19)

2010年(1160)

2009年(969)

2008年(153)

分类: Mysql/postgreSQL

2010-01-26 23:56:28

首先,分析以下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;
阅读(601) | 评论(1) | 转发(0) |
0

上一篇:MYSQL学习笔记-索引

下一篇:try-except-else

给主人留下些什么吧!~~

chinaunix网友2010-07-20 11:41:39

把数据全select into file 然后备份 空表alter table 最后load in file 先把数据导出来然后再导进来