Mysql 大批量插入优化

1、合并数据
  将多个insert合并为一个insert,因为每一条insert,都会产生日志记录,合并后可以减少日志刷盘的量和频率。

  注意:一条SQL的长度限制,可以修改max_allow_packet的值(默认是1M)。

2、放入事务中处理

  不显示开启事务,mysql会内部自动创建事务,如果将多个insert放入一个显示开启的事务中处理,能有效降低事务创建的消耗。

  注意:要控制数据数量的大小,写入日志大小超过innodb_log_buffer_size后,在事务提交前会先将日志刷到磁盘中,效率会下降。

  The size in bytes of the buffer that InnoDB uses to write to the log files on dis.

  The deault value is 8M.A large log buffer enables large transactions to run without a need to write the log to disk before the transactions commit.

  Thus, if you have transactions that update, insert, or delete many rows, making the log buffer larger saves disk I/O.

3、顺序插入

  尽量创建一个有序主键,插入的数据也按照先后顺序来插入,可以减少数据索引的创建和数据写入磁盘的工作量。

posted @ 2018-03-29 17:07  lion_eagle  阅读(1334)  评论(0编辑  收藏  举报