简体   繁体   English

Mysql 5.5 Innodb慢插入查询问题

[英]Mysql 5.5 Innodb slow insert query issue

I have a stored procedure which inserts around 3K rows in a temporary table. 我有一个存储过程,在临时表中插入大约3K行。 This SP used to take 3 seconds on Mysql 5.1 (windows desktop), the same SP was taking around 2 mins on Mysql 5.5. 这个SP在Mysql 5.1(Windows桌面)上花了3秒钟,同样的SP在Mysql 5.5上大约需要2分钟。

I tried lot of things like optimizing innodb by changing innodb_buffer_pool_size etc but nothing worked and then I read something on stackoverflow which solved my problem. 我尝试了很多东西,比如通过改变innodb_buffer_pool_size等优化innodb,但没有任何效果,然后我在stackoverflow上读了一些东西解决了我的问题。

START TRANSACTION; 
CALL sp();
COMMIT;

The above code solved the issue and now I am back to 3 secs execution time. 上面的代码解决了这个问题,现在我回到了3秒的执行时间。 Can someone please tell me what exactly is happening here. 有人可以告诉我这里到底发生了什么。 Why do I have to add start transaction in 5.5 for fast execution? 为什么我必须在5.5中添加启动事务才能快速执行? Why didnt I need this in 5.1 为什么我在5.1中不需要这个

I would guess it's due to autocommit being disabled by running 我猜这是由于自动提交被运行禁用

START TRANSACTION; 

You can verify it by disabling autocommit manually and then run your proc. 您可以通过手动禁用自动提交验证它,然后运行您的proc。

SET autocommit=0;

I added the following line in my mysql.ini file 我在mysql.ini文件中添加了以下行

innodb_flush_log_at_trx_commit=0

It seems to have solved the issue, the procedure runs fast now. 它似乎解决了这个问题,程序现在运行得很快。 Ideally innodb_flush_log_at_trx_commit should be 1 for ACID compliance. 理想情况下,对于ACID合规性,innodb_flush_log_at_trx_commit应为1。

When you insert a row into an InnoDB table, MySQL builds the primary_key and updates indexes for that row. 当您在InnoDB表中插入行时,MySQL会构建primary_key并更新该行的索引。 This is done for each row and can slow down your inserts considerably. 这是针对每一行完成的,可以大大减慢插入速度。 By wrapping them up in a transaction, you're essentially forcing it to first insert all the rows, and then rebuild keys and indexes in one shot. 通过将它们包装在事务中,您实际上是强制它首先插入所有行,然后一次性重建键和索引。 Hence the performance gain. 因此性能提升。

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

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