mysql:insert插入数据过慢如何解决,设置innodb_flush_log_at_trx_commit为0就能解决

问题:

最近在做性能测试,造数据,发现insert好慢,只有几十条每秒,很奇怪,最后再网上找到了原因。

网文如下:

MY SQL insert 速度过慢
最近在用MySQL做存储,测试中发现插入数据太慢了,插入速度只有20-30 条/秒,后来查资料后,将MySQL的1个参数:innodb_flush_log_at_trx_commit,1改为了0(修改方法,可以直接修改my.ini(windows)/my.cnf(linux)中的对应参数,似乎用命令直接设置没有效果),插入速度就提升到了3000+每S,MySQL的文档中,对这个参数的描述如下:
If the value of innodb_flush_log_at_trx_commit is 0, the log buffer is written out to the log file once per second and the flush to disk operation is performed on the log file, but nothing is done at a transaction commit. When the value is 1 (the default), the log buffer is written out to the log file at each transaction commit and the flush to disk operation is performed on the log file. When the value is 2, the log buffer is written out to the file at each commit, but the flush to disk operation is not performed on it. However, the flushing on the log file takes place once per second also when the value is 2. Note that the once-per-second flushing is not 100% guaranteed to happen every second, due to process scheduling issues. 

这样,如果transaction commit的频率非常高,1秒钟内会进行很多次的话,可以考虑将其设置为0,但这样就要承担数据库Crash后,1秒内未存储到数据库数据丢失可能的风险


设置后重启mysql,再重新执行存储过程,基本上插入速度能达到6000条/s。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
***很慢的原因有多种可能性。其中一个可能的原因是事务的自动提交模式开启,导致每次插入都会触发一次提交操作,从而降低了插入数据的速度。将自动提交关闭,并通过程序来控制提交的时机,可以提高插入数据的效率。 此外,索引、唯一性检查和数据大小也是影响插入速度的主要因素。在插入大量数据时,可以考虑暂时禁用索引和唯一性检查,待数据插入完成后再重新启用。同时,确保数据大小合理,不要一次性插入过多的数据,可以分批进行插入操作,以减少单次插入的负载。 另外,还有其他一些方法可以优化mysql批量插入的速度,比如使用LOAD DATA INFILE语句、使用INSERT INTO ... SELECT语句等。具体的方法选择可以根据实际情况和需求来确定。最好的方式是结合多种方法来优化插入速度,以达到最佳效果。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [Mysql批量插入数据问题解决和优化](https://blog.csdn.net/u010886217/article/details/106406758)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [4、提高插入数据的速度](https://blog.csdn.net/weixin_44234912/article/details/109095232)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值