In SQLite, an Index is a pointer to specific columns in a table that enable super-fast retrieval.
Note: The database size can increase significantly, however if performance is more important, the tradeoff can be worth it.
Indexes are defined using the indexGroups() property of the @Table annotation. These operate similar to how UniqueGroup work:
1. specify an @IndexGroup , giving it a number and name . The name is used in the database directly to create an index.
2. Add the @Index annotation to a @Column and assign the indexGroups to the number you specified in the annotation.
3. Build and an IndexProperty gets generated. This allows super-easy access to the index so you can enable/disable it with ease.
Note: Index are not explicitly enabled unless coupled with an IndexMigration. (read here).
You can define as many @IndexGroup you want within a @Table as long as one field references the group. Also individual @Column can belong to any number of groups:
IndexModel2_Table.firstIndex.createIfNotExists(database);
(select from IndexModel2::class
indexedBy IndexModel2_Table.firstIndex
where ...)
IndexModel2_Table.firstIndex.drop(database); // turn it off when no longer needed.
val index = indexOn<SomeTable>("MyIndex", SomeTable_Table.name, SomeTable_Table.othercolumn)
index.createIfNotExists(database)
index.drop(database)