2

I have run a few trials and there seems to be some improvement in speed if I set autocommit to False.

However, I am worried that doing one commit at the end of my code, the database rows will not be updated. So, for example, I do several updates to the database, none are committed, does querying the database then give me the old data? Or, does it know it should commit first?

Or, am I completely mistaken as to what commit actually does?

Note: I'm using pyodbc and MySQL. Also, the table I'm using are InnoDB, does that make a difference?

3 Answers 3

3

There are some situations which trigger an implicit commit. However under most situations not commiting means data will be unavailable to other connections.

It also means that if another connection tries to perform an action that conflicts with an ongoing transaction (another connection locked that resource) the last request will have to wait for the lock to be released.

As for performance concerns, autocommit causes every change to be immediate. The performance hit will be quite noticeable on big tables as on each commit indexes and constraints need to be updated/checked too. If you only commit after a series of queries, indexes/constraints will only be updated at that time.

On the other hand, not committing frequently enough might cause the server to have too much work trying to maintain consistency between the two sets of data. So there is a trade-off.

And yes, using InnoDB makes a difference. If you were using for instance MyISAM you wouldn't have transactions at all, so any change will be permanent (similarly to autocommit=True). On MyISAM you can play with the delay-key-write option.

For more information about transactions have a look at the official documentation. For more tips about optimization have a look at this article.

1

The default transaction mode for InnoDB is REPEATABLE READ, all the read will be consistent within a transaction. If you insert rows and query them in the same transaction, you will not see the newly inserted row, but they will be stored when you commit the transaction.
If you want to see the newly inserted row before you commit the transaction, you can set the isolation level to READ COMMITTED.

0

As long as you use the same connection, the database should show you a consistent view on the data, e.g. with all changes made so far in this transaction.

Once you commit, the changes will be written to disk and be visible to other (new) transactions and connections.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.