I'm trying to optimize my DB calls for a long rake task so I've been analyzing each and every query.
我正在尝试优化我的DB调用,以进行长rake任务,所以我一直在分析每一个查询。
I noticed that Rails often wraps my inserts and updates with BEGIN
and COMMIT
. I'm not using .transaction
anywhere so I'm confused why this is happening. I've disabled my after_save
and after_commit
logging but that doesn't seem to have an effect.
我注意到Rails经常以开始和提交来包装我的插入和更新。我没有在任何地方使用。transaction,所以我很困惑为什么会这样。我已经禁用了after_save和after_commit日志记录,但这似乎没有效果。
Any ideas? Amazon Web Services measures every single MySQL I/O so I want to get rid of these BEGIN
and COMMIT
statements.
什么好主意吗?Amazon Web Services度量每一个MySQL I/O,因此我想去掉这些BEGIN和COMMIT语句。
Thanks!
谢谢!
3 个解决方案
#1
4
Rails wraps each write in a transaction. Example:
Rails在事务中包装每个写入。例子:
Foo.create
Foo.create
Foo.create
Log:
日志:
(0.1ms) BEGIN
SQL (6.9ms) INSERT INTO `foos` VALUES ()
(3.3ms) COMMIT
(0.2ms) BEGIN
SQL (8.0ms) INSERT INTO `foos` VALUES ()
(0.4ms) COMMIT
(0.2ms) BEGIN
SQL (7.3ms) INSERT INTO `foos` VALUES ()
(1.3ms) COMMIT
If you wrap these calls in an explicit transaction, Rails will use that transaction instead of creating new ones:
如果在显式事务中包装这些调用,Rails将使用该事务而不是创建新事务:
Foo.transaction do
Foo.create
Foo.create
Foo.create
end
Log:
日志:
(0.2ms) BEGIN
SQL (0.3ms) INSERT INTO `foos` VALUES ()
SQL (0.2ms) INSERT INTO `foos` VALUES ()
SQL (0.2ms) INSERT INTO `foos` VALUES ()
(6.7ms) COMMIT
#2
3
You don't actually want to get rid of them. Active Record does a lot of magic behind the scenes, so when it comes to saving complex models/relationships, the transaction wrapper is very useful for undoing database changes if something goes wrong.
其实你并不想摆脱它们。Active Record在幕后做了很多神奇的工作,所以当涉及到保存复杂的模型/关系时,包装器事务对于在发生错误时取消数据库更改非常有用。
Note, this has nothing to do with you using .transaction
. Active Record is automatically wrapping common actions like .save
and .update_attribute
in a database transaction.
注意,这与使用.transaction无关。活动记录自动封装数据库事务中的.save和.update_attribute等公共操作。
#3
2
BEGIN and COMMIT are not an overload but are the saviours I must say. They helps you to rollback your transactions if and when something goes wrong. Though you have not used .transaction
they are by default implemented as the background Magic of Rails.
开始和承诺并不是超负荷,而是我必须说的救世主。当出现问题时,它们可以帮助您回滚事务。虽然您还没有使用.transaction,但默认情况下它们是作为Rails的后台魔法实现的。
If you really want to save your buck on AWS service try for removing sumthing which does not hamper your application security or robustness.
如果您真的想在AWS服务上节省开支,请尝试删除不影响应用程序安全性或健壮性的总和。
#1
4
Rails wraps each write in a transaction. Example:
Rails在事务中包装每个写入。例子:
Foo.create
Foo.create
Foo.create
Log:
日志:
(0.1ms) BEGIN
SQL (6.9ms) INSERT INTO `foos` VALUES ()
(3.3ms) COMMIT
(0.2ms) BEGIN
SQL (8.0ms) INSERT INTO `foos` VALUES ()
(0.4ms) COMMIT
(0.2ms) BEGIN
SQL (7.3ms) INSERT INTO `foos` VALUES ()
(1.3ms) COMMIT
If you wrap these calls in an explicit transaction, Rails will use that transaction instead of creating new ones:
如果在显式事务中包装这些调用,Rails将使用该事务而不是创建新事务:
Foo.transaction do
Foo.create
Foo.create
Foo.create
end
Log:
日志:
(0.2ms) BEGIN
SQL (0.3ms) INSERT INTO `foos` VALUES ()
SQL (0.2ms) INSERT INTO `foos` VALUES ()
SQL (0.2ms) INSERT INTO `foos` VALUES ()
(6.7ms) COMMIT
#2
3
You don't actually want to get rid of them. Active Record does a lot of magic behind the scenes, so when it comes to saving complex models/relationships, the transaction wrapper is very useful for undoing database changes if something goes wrong.
其实你并不想摆脱它们。Active Record在幕后做了很多神奇的工作,所以当涉及到保存复杂的模型/关系时,包装器事务对于在发生错误时取消数据库更改非常有用。
Note, this has nothing to do with you using .transaction
. Active Record is automatically wrapping common actions like .save
and .update_attribute
in a database transaction.
注意,这与使用.transaction无关。活动记录自动封装数据库事务中的.save和.update_attribute等公共操作。
#3
2
BEGIN and COMMIT are not an overload but are the saviours I must say. They helps you to rollback your transactions if and when something goes wrong. Though you have not used .transaction
they are by default implemented as the background Magic of Rails.
开始和承诺并不是超负荷,而是我必须说的救世主。当出现问题时,它们可以帮助您回滚事务。虽然您还没有使用.transaction,但默认情况下它们是作为Rails的后台魔法实现的。
If you really want to save your buck on AWS service try for removing sumthing which does not hamper your application security or robustness.
如果您真的想在AWS服务上节省开支,请尝试删除不影响应用程序安全性或健壮性的总和。