“COMMIT” in mysql slow query log with rails application 2.3.5
-
27-10-2019 - |
문제
I have lots of entries in the slow query log that just have "COMMIT" on them, they take around 3 seconds, which is slow but not awful compared to some other queries.
the entries look like this:
COMMIT;
# Time: 120111 14:11:05
# User@Host: root[root] @ localhost []
# Query_time: 3.140228 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0
SET timestamp=1326291065;
COMMIT;
# Time: 120111 14:14:03
# User@Host: root[root] @ localhost []
# Query_time: 2.036250 Lock_time: 0.000000 Rows_sent: 0 Rows_examined: 0
SET timestamp=1326291243;
COMMIT;
My question is why would rails be issuing a "COMMIT"?
My understanding is it should be using autocommit unless I am doing transactions manually.
해결책
Rails performing a transaction in many case.
For instance if you save a record with a nested model (like topic which has many comments), Rails will wrap updates (1 topic and n comments) in a transaction to keep your database consistent.
EDIT
As soon as you have two or more queries linked to each other that changed the database you must have a transaction, but it seems that Rails perform a begin/commit even on single queries. I think it's designed this way to avoid useless auto-commit activation/deactivation.