disable keys的问题

小小小小周 2011-05-23 05:57:19
MYisam表,加载数据。书上有资料说可以用
alter table tbname disable kes;
加载数据;
alter table tbname enable keys;
可以提高加载速度。
上面的keys都不是普通索引,不是primary key 和unique key。

表数据1000W,在有索引的时候,我load 10W条数据,耗时 5s,
按照上面的方法:
加载进去只消耗了0.38s,但是在enable keys的时候花了7分钟,

问:这种方法提高了效率,为什么整个过程时间花的更久?在enable的时候查看到的线程状态是:Repair with keycache。

用这方法加载数据得到一个无碎片,紧凑的索引,而直接在有索引的表中load data的话,是不是索引会出现碎片?要是真出现索引碎片的话,我用optimize table也只需要30秒,根本不会超过1分钟。所以我觉得虽然disable kyes 可以提高索引,但是开启索引的话,消耗大时间不能忍受。。

大家都采用什么方法来提高load data 呢?
...全文
445 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zuoxingyu 2011-05-24
  • 打赏
  • 举报
回复
[Quote=引用 13 楼 mr_mablevi 的回复:]

引用 12 楼 zuoxingyu 的回复:
在大批量插入的时候,你试试下面2个参数
1:INSERT DELAYED ,这个插入不需要等待MYSQL处理,直接进入系统缓存队列
2:加大bulk_insert_buffer_size值
MyISAM uses a special tree-like cache to make bulk inserts faster for INSERT ……
[/Quote]

测试一下不用load data,直接insert delayed,看看哪个快。
不过insert delayed可能会发生丢数据的情况。
zuoxingyu 2011-05-24
  • 打赏
  • 举报
回复
DISABLE KEY AND ENABLE KEY
如果在ENABLE KEY的过程中宕机了,MYISAM的索引文件很可能被损坏掉,你得再修复。

可以说说你们为什么不用INNODB么?
小小小小周 2011-05-24
  • 打赏
  • 举报
回复
[Quote=引用 12 楼 zuoxingyu 的回复:]
在大批量插入的时候,你试试下面2个参数
1:INSERT DELAYED ,这个插入不需要等待MYSQL处理,直接进入系统缓存队列
2:加大bulk_insert_buffer_size值
MyISAM uses a special tree-like cache to make bulk inserts faster for INSERT ... SELECT, INSERT ... VA……
[/Quote]

第2个参数,我加了一点,但是如何知道加大这个值合适不合适呢?
第1个参数,加不上,我数据导入直接用load data的
zuoxingyu 2011-05-24
  • 打赏
  • 举报
回复
在大批量插入的时候,你试试下面2个参数
1:INSERT DELAYED ,这个插入不需要等待MYSQL处理,直接进入系统缓存队列
2:加大bulk_insert_buffer_size值
MyISAM uses a special tree-like cache to make bulk inserts faster for INSERT ... SELECT, INSERT ... VALUES (...), (...), ..., and LOAD DATA INFILE when adding data to nonempty tables. This variable limits the size of the cache tree in bytes per thread. Setting it to 0 disables this optimization. The default value is 8MB.
小小小小周 2011-05-24
  • 打赏
  • 举报
回复
[Quote=引用 10 楼 zuoxingyu 的回复:]
This feature can be activated explicitly for a MyISAM table. ALTER TABLE ... DISABLE KEYS tells MySQL to stop updating nonunique indexes. ALTER TABLE ... ENABLE KEYS then should be used to re-create m……
[/Quote]

没有唯一索引。。
zuoxingyu 2011-05-24
  • 打赏
  • 举报
回复
This feature can be activated explicitly for a MyISAM table. ALTER TABLE ... DISABLE KEYS tells MySQL to stop updating nonunique indexes. ALTER TABLE ... ENABLE KEYS then should be used to re-create missing indexes. MySQL does this with a special algorithm that is much faster than inserting keys one by one, so disabling keys before performing bulk insert operations should give a considerable speedup. Using ALTER TABLE ... DISABLE KEYS requires the INDEX privilege in addition to the privileges mentioned earlier.

While the nonunique indexes are disabled, they are ignored for statements such as SELECT and EXPLAIN that otherwise would use them.

手册里说的是nonunique key,非唯一索引。
你前面用的是覆盖索引,里面有包含唯一性索引么??
zuoxingyu 2011-05-24
  • 打赏
  • 举报
回复
我搜了下,下面这个情况和你的一样。看看
http://forums.mysql.com/read.php?21,68820,68939#msg-68939
When you enable keys, it will have to rebuilt the whole index including the existing records, not just the newly inserted records, since it does not know which records are newly inserted.

Maybe you should increase the bulk_insert_buffer_size

不知道是否正确。
小小小小周 2011-05-23
  • 打赏
  • 举报
回复
[Quote=引用 5 楼 rucypli 的回复:]
http://blog.sina.com.cn/s/blog_6238358c0100ppzz.html


你的enable 7分钟 貌似不太对劲
[/Quote]

我看了下,他就一个字段的索引,我的是全部字段都索引了(覆盖索引).可能这点问题.明天再试试..
小小小小周 2011-05-23
  • 打赏
  • 举报
回复
感谢 狼头大哥和rucypli兄弟的回复,明天我去公司再试试,公司只能上csdn论坛,其他什么网站都上不去,哎.查找不了任何资料.到时候再请你们指点..
回南山种豆 2011-05-23
  • 打赏
  • 举报
回复
学习,
rucypli 2011-05-23
  • 打赏
  • 举报
回复
http://blog.sina.com.cn/s/blog_6238358c0100ppzz.html


你的enable 7分钟 貌似不太对劲

ACMAIN_CHM 2011-05-23
  • 打赏
  • 举报
回复
肯定是这样,数据越多,生成期索引的时间需要越多。
小小小小周 2011-05-23
  • 打赏
  • 举报
回复
请问狼头大哥,表数据越大,是不是load data 消耗大时间越久。我测试下来,每load 10W条数据,消耗的时间就每增加0.5s-1s。不知我得到的这个结论可靠不。
ACMAIN_CHM 2011-05-23
  • 打赏
  • 举报
回复
应该在大部情况下,先大批量插入数据,然后再生成索引会节约时间。 建议楼主多测试几次,然后看平均时间。
小小小小周 2011-05-23
  • 打赏
  • 举报
回复
上面的keys都普通索引,不是primary key 和unique key

56,677

社区成员

发帖
与我相关
我的任务
社区描述
MySQL相关内容讨论专区
社区管理员
  • MySQL
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧