Skip to content

Creating Indexes

Dominic Fischer edited this page Jul 21, 2017 · 2 revisions

Also there are these things called indexes. If a column or columns are often filtered or sorted against, you might want to consider creeating an index for them. They often make queries much faster.

An index can be created using the CreateIndex method in the SQLiteDatabase class.

For example, since we made a VIEW to join Albums and Tracks, it would be a good idea to create an index on the AlbumId column of the Track table.

The generic argument is the model class used for the table.
The first argument is the name of the index.
The second argument whether the index is unique. (In this case it is false because multiple Tracks can have the same AlbumId).
The third argument is a lambda describing the columns to be included in the index.

CreateIndex<Track>("Track_AlbumId", false, t => t.AlbumId);

Just in case you were wondering, I didn't create an index on the AlbumId column on the Album table because SQLite implicitly creates an index for PRIMARY KEY columns and columns declared as UNIQUE.
Also indexes can take up a lot of space, so don't go creating one for every combination of columns you have...... unless you need to. :D

Clone this wiki locally