mercredi 27 avril 2016

Correctly using indexes on Android Sqlite3 tables?

I want to make my queries reasonably efficient, and based on my research so far, in SQL this is done by using indexes.

For example say I have this table

CREATE TABLE MYTABLE 
    (_ID INTEGER PRIMARY KEY AUTOINCREMENT,
    NAME_ID INTEGER,
    SOME_VALUE REAL,
    TIMESTAMP TEXT)

From what I am reading, indexes are automatically created for primary key fields (so _ID is indexed) and UNIQUE fields, but sometimes there may be multiple values for a given NAME_ID, SOME_VALUE, or TIMESTAMP so I cannot apply UNIQUE to any of them.

One common query might be to select rows in this table with NAME_ID = x and date1 <= TIMESTAMP <= date2'. Is it correct to say that I should make indexes for both NAME_ID and TIMESTAMP? Or would I make a composite index:

CREATE INDEX MYTABLE_INDEX on MYTABLE (NAME_ID, TIMESTAMP);

or is this the incorrect way to go about it?

If I create these indexes, am I basically done? Have I done all I need to help ensure that future queries on this table will be efficient if I am basing criteria on NAME_ID and TIMESTAMP like I've shown above?

Aucun commentaire:

Enregistrer un commentaire