Note: these pages are no longer maintainedNever the less, much of the information is still relevant.Beware, however, that some of the command syntax is from older versions, and thus may no longer work as expected. Also: external links, from external sources, inside these pages may no longer function. |
Recipe #9: |
|
2011 January 28 |
Previous Slide | Table of Contents | Next Slide |
ACID has nothing to do with chemistry (pH, hydrogen
and hydroxide ions and so on). In the DBMS context this one is an acronym meaning:
|
BEGIN; CREATE TABLE test ( num INTEGER, string TEXT); INSERT INTO test (num, string) VALUES (1, 'aaaa'); INSERT INTO test (num, string) VALUES (2, 'bbbb'); INSERT INTO test (num, string) VALUES (3, 'cccc'); |
SELECT * FROM test; |
Anyway, some relevant consequence arises from the initial
BEGIN declaration:
|
COMMIT; |
ROLLBACK; |
Performance Hints Handling TRANSACTIONs seems too much complex to you ? so you are thinking "I'll simply ignore all this ..."Well, carefully consider that SQLite is a full ACID DBMS, so it's purposely designed to handle TRANSACTIONs. And that's not all. SQLite actually is completely unable to operate outside a TRANSACTION context. Each time you miss to explicitly declare some BEGIN / COMMIT, then SQLite implicitly enters the so called AUTOCOMMIT mode: each single statement will be handled as a self-standing TRANSACTION. i.e. when you declare e.g. some simple INSERT INTO ... statement, then SQLite silently translates this into: BEGIN; INSERT INTO ...; COMMIT; Please note well: this is absolutely safe and acceptable when you are inserting few rows by hand-writing. But when some C / C++ / Java / Python process attempts to INSERT many and many rows (maybe many million rows), this will impose an unacceptable overhead. In other words, your process will perform very poorly, taking an unneeded long time to complete: and all this is simply caused by not declaring an explicit TRANSACTION. The strongly suggested way to perform fast INSERTs (UPDATE, DELETE ...) is the following one:
|
Connectors oddities (true life tales) Developers, be warned: different languages, different connectors, different default settings ...C / C++ developers will directly use the SQLite's API: in this environment the developer is expected to explicitly declare TRANSACTIONs as required, by calling:
Java / JDBC connectors more or less follow the same approach: the developer is expected to explicitly quit the AUTOCOMMIT mode, then declaring a COMMIT when required and appropriate:
Omitting this step will cause very slow performance. But at least any change will surely affect the underlying DB. Python follows a completely different approach: a TRANSACTION is silently active at each time. Performance always is optimal. But forgetting to explicitly call conn.commit() before quitting, any applied change will be lost forever immediately after terminating the connection. And this may really be puzzling for beginners, I suppose. |
Previous Slide | Table of Contents | Next Slide |
Author: Alessandro Furieri a.furieri@lqt.it | |
This work is licensed under the Attribution-ShareAlike 3.0 Unported (CC BY-SA 3.0) license. | |
Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.3 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. |