27

My Android app works by using a SQLite database that is generated on the user's PC and transferred to the device. It all works, but I had not anticipated the number of users who would have really huge amounts of data. In these cases, the UI is very sluggish as it waits for the data to be fetched.

I've tried a number of tricks that I was "sure" would speed things up, but nothing seems to have any noticeable effect. My queries are almost all very simple, being usually a single "col=val" for the WHERE clause, and INTEGER data in the column. So I can't do much with the queries.

The latest, and I am not an SQL expert by any means, was to use "CREATE INDEX" commands on the PC, believing that these indexes are used to speed up database searches. The indexes increased the size of the database file significantly, so I was then surprised that it seemed to have no effect whatsoever on the speed of my app! A screen that was taking 8 seconds to fill without indexes still takes about 8 seconds even with them. I was hoping to get things down to at least half that.

What I am wondering at this point is if the SQLite implementation on Android uses database indexes at all, or if I'm just wasting space by generating them. Can anyone answer this?

Also, any other things to try to speed up access?

(For what it's worth, on an absolute basis the users have nothing to complain about. My worst-case user so far has data that generates 630,000 records (15 tables), so there's only so much that's possible!)

Doug Gordon GHCS Systems

1
  • 2
    post some slow queries, along with table definitions and index-definitions
    – Mikpa
    Nov 28, 2010 at 21:17

3 Answers 3

12

SQLite will use the index if it is appropriate for the query. Use EXPLAIN

EXPLAIN QUERY PLAN ... your select statement ...

to see what indexes SQLite is using. The query plan is based on some assumptions about your database content. You may be able to improve the plan by using ANALYZE

12

I was finally able to achieve tremendous performance gains simply by querying the database in a much more efficient way. For example, in building up an array of information, I was previously querying the database for each row that I required with a "WHERE _id = n" type selector. But in doing it this way, I was issuing a dozen or more queries, one at a time.

Instead, I now build up a list of IDs that are required, then get them all with a single query of the form "WHERE _id IN (n1, n2, n3, ...)" and iterate through the returned cursor. Doing this and some other structure optimizations, the largest database is now almost as quick to view as the more average case.

1
  • 2
    yes, reducing the mount of queries , if possible, is one of the most efficient way of improving performance. As large mount time is spent on waiting the relative slow I/O. May 11, 2011 at 12:32
5

Every time you're going to perform some kind of action (being database lookup, long-running calculation, web request etc.) taking more than a couple of hundreds of milliseconds, you should consider wrapping this inside an AsyncTask.

Painless Threading is a good article on this topic, so I recommend you take a close look at it.

This article discusses the threading model used by Android applications and how applications can ensure best UI performance by spawning worker threads to handle long-running operations, rather than handling them in the main thread.

3
  • 1
    I'm already doing this. The issue is the amount of time it takes before the requested data is finally displayed (in onPostExecute).
    – gordonwd
    Nov 28, 2010 at 20:52
  • @gordonwd: I suppose (since you say you're loading so much data) that you display some long list. If this is the case, you could try a solution as suggested in this Android Endless List question (stackoverflow.com/questions/1080811/android-endless-list). And as somebody pointed out above, you should also post some code from your tables and how you fetch data from the for us to better be able to help you.
    – Julian
    Nov 28, 2010 at 21:55
  • @Nailuj looks like your article link has changed, probably to this; android-developers.blogspot.com.au/2009/05/… but can you check and fix the 404? Feb 26, 2013 at 2:53

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.