SQLite: End transaction is taking too long

自作多情 提交于 2019-12-22 09:37:21

问题


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


回答1:


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;


来源:https://stackoverflow.com/questions/10209438/sqlite-end-transaction-is-taking-too-long

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!