简体   繁体   English

MySQL插入与选择从myisam到innodb

[英]Mysql Insert into with select from myisam to innodb

I have a table, which is accessed by multiple worker applications, which grab a batch of records, calculate some stuff on them and update the calculated result back into this table. 我有一个表,供多个工作程序访问,该工作程序抓取一批记录,在其中记录一些内容,并将计算结果更新回该表中。 This table is currently MyISAM based, but since we had some changes to our calculating algorithm and it resulted in a huge speed up, we have seen loads of locks on the table, which is expected as it's a MyISAM table. 该表当前基于MyISAM,但是由于我们对计算算法进行了一些更改,并导致了极大的提高,因此我们看到了表上的锁负载,这是MyISAM表,这是可以预期的。 (The table has around 650-700 000 records in it). (该表中大约有650-700 000条记录)。

Once all the records are processed, a report is generated based on it and the table is truncated. 处理完所有记录后,将基于该记录生成报告,并且该表将被截断。 Once the table is truncated, a processes reinitalizeses it and the cycle starts again. 该表被截断后,将重新启动该表,然后该循环再次开始。

I was thinking about switching over the MyISAM table to InnoDB to prevent locks, but the processes, which initalizes the table with a INSERT INTO .. SELECT .. FROM statement (which used to take 3-4 mintues), was running for 35-40 minutes when i decided to stop it. 我当时正在考虑将MyISAM表切换到InnoDB以防止锁定,但是使用INSERT INTO .. SELECT .. FROM语句(过去需要3-4分钟)初始化该表的进程正在运行35- 40分钟,当我决定停止它。 Why is this? 为什么是这样? Is there a way to speed it up? 有没有办法加快速度?

MyISAM stores records in the order they're inserted. MyISAM按插入顺序存储记录。 On the other hand, InnoDB's primary key is a clustered index, so records are physically stored in the same order as the primary key. 另一方面,InnoDB的主键是聚簇索引,因此记录的存储顺序与主键的存储顺序相同。 Therefore, ensure you're inserting in primary key order. 因此,请确保按主键顺序插入。 You can add an ORDER BY clause to the INSERT INTO ... SELECT. 您可以在INSERT INTO ... SELECT中添加ORDER BY子句。

Also, to avoid split pages and rebuilding of your secondary indexes, add your secondary indexes after you've inserted the initial rows. 另外,为避免拆分页面和重建二级索引,请在插入初始行后添加二级索引。 So, remove the indexes, then re-add them after. 因此,删除索引,然后再重新添加它们。

Also, to avoid the row locking (and auto-increment locking) overhead, explicitly lock the tables with LOCK TABLES. 另外,为避免行锁定(和自动增量锁定)开销,请使用LOCK TABLES显式锁定表。

Finally, increase innodb_buffer_pool_size , otherwise your indexes will build very slowly. 最后,增加innodb_buffer_pool_size ,否则您的索引将建立得很慢。 The default is only 8MB. 默认值为8MB。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM