Sapan
Sapan

Reputation: 1613

android database indexing

My android application needs frequent access to sqlite database. I was thinking if there is a way to index the database to have faster access to data. Ha anyone done indexing of database in android? Please suggest me the best way to do that?

Is it really needed as we have all the database on local device and even running query everytime will not take much time.

Rgds, Sapan

Upvotes: 4

Views: 2756

Answers (1)

Denis de Bernardy
Denis de Bernardy

Reputation: 78523

SQLite supports the CREATE INDEX statement:

http://www.sqlite.org/lang_createindex.html

e.g.:

create index foo_bar_idx on foo(bar);

For an in-depth discussion in when a query planner would decide to use an index, try the PostgreSQL documentation instead:

http://www.postgresql.org/docs/9.0/static/indexes.html

It won't work in the exact same way in SQLite, but the general ideas are the same:

  • For tiny tables, reading the whole table is faster than scanning through an index.
  • For an order by/limit, create an index on the field(s) used in the sort.
  • For a join/where, you want an index on the field(s) using in the join/where.

Upvotes: 7

Related Questions