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 呢?

解决方案 »

  1.   

    上面的keys都普通索引,不是primary key 和unique key
      

  2.   

    请问狼头大哥,表数据越大,是不是load data 消耗大时间越久。我测试下来,每load 10W条数据,消耗的时间就每增加0.5s-1s。不知我得到的这个结论可靠不。
      

  3.   

    http://blog.sina.com.cn/s/blog_6238358c0100ppzz.html
    你的enable 7分钟 貌似不太对劲
      

  4.   

    感谢 狼头大哥和rucypli兄弟的回复,明天我去公司再试试,公司只能上csdn论坛,其他什么网站都上不去,哎.查找不了任何资料.到时候再请你们指点..
      

  5.   

    我搜了下,下面这个情况和你的一样。看看
    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不知道是否正确。
      

  6.   

    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,非唯一索引。
    你前面用的是覆盖索引,里面有包含唯一性索引么??
      

  7.   

    在大批量插入的时候,你试试下面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. 
      

  8.   


    第2个参数,我加了一点,但是如何知道加大这个值合适不合适呢?
    第1个参数,加不上,我数据导入直接用load data的
      

  9.   

    DISABLE KEY AND ENABLE KEY
    如果在ENABLE KEY的过程中宕机了,MYISAM的索引文件很可能被损坏掉,你得再修复。可以说说你们为什么不用INNODB么?
      

  10.   


    测试一下不用load data,直接insert delayed,看看哪个快。
    不过insert delayed可能会发生丢数据的情况。