In my Android app, I need to get 50,000 database entries (text) and compare them with a value when the activity starts (in onCreate()
). I am doing this with the simplest way: I get the whole table from db to a cursor. However this way is too laggy. Are there any other ways to do it more effectively ?
Edit: The app is "scrabble solver" that is why I am not using WHERE
clause in my query (Take the whole data and compare it with combination of the input letters). At first I was using a big table which contains whole possible words. Now I am using 26 tables. This reduced the lag and I am making database calls on a thread - that solved a lot problems too. It is still little bit laggy but much better.
To summarize and add a bit more
EXPLAIN QUERY PLAN
http://www.sqlite.org/lang_explain.html and look for any scan
operations, these are much slower than search
operations. Uses indexes to avoid scan
operations.onCreate()
, always use an AsyncTask
, a Handler
running on a background thread or some other non-main thread.You should never read from the database in the UI thread. Use a background thread via AsyncTask or using regular threading. This will fix the UI lag issue your having.
Making the database read faster will help with bringing the data faster to the user but it's even more important that the fetching of the data does not block the user from using the app.
Check out the Following Links:
Painless Threading in Android
YouTube: Writing Zippy Android Apps
Use a WHERE clause in your SQL rather than reading the whole thing in. Then add an index on the columns in the WHERE clause.
At least you can put index on the field you compare and use WHERE
clause. If you are comparing numerics Sqlite (db engine used by Android) supports functions such as MIN
and MAX
. Also if you are comparing partial strings you can use LIKE
. For query optimization there are many resources such as this
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With