4

I am inserting a few rows with in a transaction. But when I do a 'END TRANSACTION' its taking around 250ms to execute while the 'BEGIN TRANSACTION' hardly takes around 1ms. I need to fasten the speed here to suite my application. How can I?

[edit] * A single thread is accessing the database. * I have 2 tables in this database and both these have primary key on them. * With in a transaction there is exactly one insert into each the tables. * OS - windows 7

6
  • Please, you'll have to give some information so that we can at least guess what might be causing the delay. What do the INSERTs look like, how many rows already exist in that table, what indexes exist on the table, what is the operating environment, are multiple users or threads using that sqlite database. . . Apr 18, 2012 at 12:36
  • @LarryLustig: Thanks! I have now edited.
    – omggs
    Apr 18, 2012 at 12:43
  • @omggs : Hi, still don't see how the INSERT looks like. :) Apr 18, 2012 at 12:45
  • Number of records in each of the tables and integrity constraints that exist on them? This could be indexing or constraint related. Any triggers? Apr 18, 2012 at 12:45
  • @LarryLustig: Nothing at all.
    – omggs
    Apr 18, 2012 at 12:51

1 Answer 1

7

Using the out of the box, or default, settings of sqlite, 250ms to commit a transaction makes sense. This is due to how sqlite commits your transaction. It waits for the VFS for a guarantee that the writes are committed to disk to return.

Here are a couple of possibilities to optimize.

Encapsulate more inserts per transaction

If possible, execute more inserts per transaction. Try running, say, 100 inserts in one transactions, and see how little difference there will be compared to only 1 insert. You may not even see any (i.e. 100 inserts will probably take marginally more than 250ms).

Bottom line is that you'll get more bang for your buck as each insert will ultimately take less time (on average).

Use WAL journaling

I strongly recommend you try WAL journaling as you should see a dramatic reduction from 250ms. WAL shouldn't be any less safe than regular journaling. The reason WAL is faster is in its name: it appends to a journaling file instead of having the database file absorb the changes of your commit every time you commit. Read this for the full story.

To activate WAL journaling, set the journal_mode pragma to WAL:

PRAGMA journal_mode = WAL;

Change the synchronous pragma

This may or may not be good enough for you as it is less safe. As such, I recommend it only if you understand what the risks are, and also if the two previous suggestions are not good enough for you, or if you cannot use them.

Basically, changing the synchronous pragma to NORMAL or OFF will cause sqlite to not wait after the VFS for a guarantee that the writes are committed to disk to return.

Please read the documentation first, then if you still want to try it, you can set your pragma to either OFF or NORMAL:

PRAGMA synchronous = NORMAL;

or

PRAGMA synchronous = OFF;
2
  • Thanks!!!!! I chose PRAGMA synchronous = NORMAL; & PRAGMA journal_mode = WAL; for my actual application. End transaction now hardly takes time!
    – omggs
    Apr 19, 2012 at 6:12
  • How to use PRAGMA synchronous in java??
    – AnkitRox
    Nov 28, 2012 at 5:05

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

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

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