Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

When should I do certain SQLite operations on another thread(not the main thread)?

Tags:

android

sqlite

My Android application includes an SQLite database with an SQLiteOpenHelper class to help manage it. During application use, the user may perform some operations such as adding/deleting/updating etc on the database.

At some points the size of the operation will be known, like this:

  1. user clicks button to save item
  2. the SQLiteDatabase performs a single insert query
  3. user continues using app

At other areas of the app, the operation may be large, like inserting 10+ items into the database all at once.

Questions:

  • should I thread simple operations like inserting/updating/deleting/viewing 1 item?
  • will it take longer to insert 1 item into a table which contains many items(like 30+) than it would take to insert into a table with no items?
  • if i don't need to thread such simple operations, at what point do you suggest i start threading them?

when i say thread i mean using a thread that is not the main UI thread.

edit: I realize that small operations do not take much time and i could very well get away with doing them on the main thread. I am just concerned that it would be bad practice to be executing them on the main thread and would like clarification!

like image 212
james Avatar asked Feb 18 '11 22:02

james


People also ask

Can multiple processes access the same SQLite database?

SQLite allows multiple processes to have the database file open at once, and for multiple processes to read the database at once. When any process wants to write, it must lock the entire database file for the duration of its update. But that normally only takes a few milliseconds.

How thread safe is SQLite table level lock or DB level lock?

The simple answer is NO. Its not thread safe - that's it.

Can SQLite read and write at same time?

First, by default, multiple processes can have the same SQLite database open at the same time, and several read accesses can be satisfied in parallel. In case of writing, a single write to the database locks the database for a short time, nothing, even reading, can access the database file at all.

How much data can SQLite can handle?

SQLite supports databases up to 281 terabytes in size, assuming you can find a disk drive and filesystem that will support 281-terabyte files. Even so, when the size of the content looks like it might creep into the terabyte range, it would be good to consider a centralized client/server database.


1 Answers

General rule for everything: If it's fast enough, do it on the main thread. If not, use a worker thread.

Unless you have a ridiculously huge database, a single operation almost never warrants a separate thread. Databases in general are designed to scale well, but of course a very big database (10,000+ rows?) will be a bit slower than a small one. 30 rows, however, is nothing.

I would start threading stuff if you have a lot of operations going on, like a bunch of queries, or complicated queries that span several tables.

As with everything - profile your app, and if it's too slow, optimize. Don't write an awesome synchronized super-duper multi-core-ready database handler if none of your queries takes longer than 2ms.

like image 195
EboMike Avatar answered Oct 09 '22 19:10

EboMike