Adding an index usually speeds up your select queries but will slow down other queries like
insert or update. You should be careful when adding indices to ensure that this additional cost
is worth the gain.
There are 2 ways to define an index in an Entity. You can either set
index() property to index individual fields or define composite indices via
indices().
If an indexed field is embedded into another Entity via Embedded, it is NOT
added as an index to the containing Entity. If you want to keep it indexed, you must
re-declare it in the containing Entity.
Similarly, if an Entity extends another class, indices from the super classes are
NOT inherited. You must re-declare them in the child Entity or set
inheritSuperIndices() to true.
Name of the index. If not set, Room will set it to the list of columns joined by '_' and
prefixed by "index_${tableName}". So if you have a table with name "Foo" and with an index
of {"bar", "baz"}, generated index name will be "index_Foo_bar_baz". If you need to specify
the index in a query, you should never rely on this name, instead, specify a name for your
index.
Returns
String
The name of the index.
unique
boolean unique ()
If set to true, this will be a unique index and any duplicates will be rejected.
Returns
boolean
True if index is unique. False by default.
value
String[] value ()
List of column names in the Index.
The order of columns is important as it defines when SQLite can use a particular index.
See SQLite documentation for details on
index usage in the query optimizer.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-02-10 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-02-10 UTC."],[],[],null,["# Index\n\nSummary: [Methods](#pubmethods) \\| [Inherited Methods](#inhmethods) \n\nIndex\n=====\n\n| The `android.arch` Architecture Components packages are no longer maintained. They have been superseded by the corresponding [androidx.\\*](/jetpack/androidx/migrate) packages. See [androidx.room.Index](/reference/androidx/room) instead.\n\n\n`\npublic\n\n\nabstract\n@interface\nIndex\n`\n\n\n`\n\n\nimplements\n\nAnnotation\n\n\n`\n\n|-------------------------------------|\n| android.arch.persistence.room.Index |\n\n\u003cbr /\u003e\n\n*** ** * ** ***\n\nDeclares an index on an Entity.\nsee: [SQLite Index Documentation](https://sqlite.org/lang_createindex.html)\n\n\nAdding an index usually speeds up your select queries but will slow down other queries like\ninsert or update. You should be careful when adding indices to ensure that this additional cost\nis worth the gain.\n\n\nThere are 2 ways to define an index in an [Entity](/reference/android/arch/persistence/room/Entity). You can either set\n[index()](/reference/android/arch/persistence/room/ColumnInfo#index()) property to index individual fields or define composite indices via\n[indices()](/reference/android/arch/persistence/room/Entity#indices()).\n\n\nIf an indexed field is embedded into another Entity via [Embedded](/reference/android/arch/persistence/room/Embedded), it is **NOT**\nadded as an index to the containing [Entity](/reference/android/arch/persistence/room/Entity). If you want to keep it indexed, you must\nre-declare it in the containing [Entity](/reference/android/arch/persistence/room/Entity).\n\n\nSimilarly, if an [Entity](/reference/android/arch/persistence/room/Entity) extends another class, indices from the super classes are\n**NOT** inherited. You must re-declare them in the child [Entity](/reference/android/arch/persistence/room/Entity) or set\n[inheritSuperIndices()](/reference/android/arch/persistence/room/Entity#inheritSuperIndices()) to `true`.\n\nSummary\n-------\n\n| ### Public methods ||\n|-------------|------------------------------------------------------------------------------------------------------------------------------------------------------|\n| ` String` | ` `[name](/reference/android/arch/persistence/room#name())`() ` Name of the index. |\n| ` boolean` | ` `[unique](/reference/android/arch/persistence/room#unique())`() ` If set to true, this will be a unique index and any duplicates will be rejected. |\n| ` String[]` | ` `[value](/reference/android/arch/persistence/room#value())`() ` List of column names in the Index. |\n\n| ### Inherited methods |\n|-----------------------|---|\n| From interface ` java.lang.annotation.Annotation ` |-----------------------------------------|-------------------------| | ` abstract Class\u003c? extends Annotation\u003e` | ` annotationType() ` | | ` abstract boolean` | ` equals(Object arg0) ` | | ` abstract int` | ` hashCode() ` | | ` abstract String` | ` toString() ` | ||\n\nPublic methods\n--------------\n\n### name\n\n```\nString name ()\n```\n\nName of the index. If not set, Room will set it to the list of columns joined by '_' and\nprefixed by \"index_${tableName}\". So if you have a table with name \"Foo\" and with an index\nof {\"bar\", \"baz\"}, generated index name will be \"index_Foo_bar_baz\". If you need to specify\nthe index in a query, you should never rely on this name, instead, specify a name for your\nindex.\n\n\u003cbr /\u003e\n\n| Returns ||\n|----------|-------------------------------|\n| `String` | The name of the index. \u003cbr /\u003e |\n\n### unique\n\n```\nboolean unique ()\n```\n\nIf set to true, this will be a unique index and any duplicates will be rejected.\n\n\u003cbr /\u003e\n\n| Returns ||\n|-----------|---------------------------------------------------|\n| `boolean` | True if index is unique. False by default. \u003cbr /\u003e |\n\n### value\n\n```\nString[] value ()\n```\n\nList of column names in the Index.\n\n\nThe order of columns is important as it defines when SQLite can use a particular index.\nSee [SQLite documentation](https://www.sqlite.org/optoverview.html) for details on\nindex usage in the query optimizer.\n\n\u003cbr /\u003e\n\n| Returns ||\n|------------|-----------------------------------------------|\n| `String[]` | The list of column names in the Index. \u003cbr /\u003e |\n\n-\n\n Annotations\n -----------\n\n - [ColumnInfo](/reference/android/arch/persistence/room/ColumnInfo)\n - [ColumnInfo.Collate](/reference/android/arch/persistence/room/ColumnInfo.Collate)\n - [ColumnInfo.SQLiteTypeAffinity](/reference/android/arch/persistence/room/ColumnInfo.SQLiteTypeAffinity)\n - [Dao](/reference/android/arch/persistence/room/Dao)\n - [Database](/reference/android/arch/persistence/room/Database)\n - [Delete](/reference/android/arch/persistence/room/Delete)\n - [Embedded](/reference/android/arch/persistence/room/Embedded)\n - [Entity](/reference/android/arch/persistence/room/Entity)\n - [ForeignKey](/reference/android/arch/persistence/room/ForeignKey)\n - [ForeignKey.Action](/reference/android/arch/persistence/room/ForeignKey.Action)\n - [Ignore](/reference/android/arch/persistence/room/Ignore)\n - [Index](/reference/android/arch/persistence/room)\n - [Insert](/reference/android/arch/persistence/room/Insert)\n - [OnConflictStrategy](/reference/android/arch/persistence/room/OnConflictStrategy)\n - [PrimaryKey](/reference/android/arch/persistence/room/PrimaryKey)\n - [Query](/reference/android/arch/persistence/room/Query)\n - [RawQuery](/reference/android/arch/persistence/room/RawQuery)\n - [Relation](/reference/android/arch/persistence/room/Relation)\n - [SkipQueryVerification](/reference/android/arch/persistence/room/SkipQueryVerification)\n - [Transaction](/reference/android/arch/persistence/room/Transaction)\n - [TypeConverter](/reference/android/arch/persistence/room/TypeConverter)\n - [TypeConverters](/reference/android/arch/persistence/room/TypeConverters)\n - [Update](/reference/android/arch/persistence/room/Update)\n-\n\n Classes\n -------\n\n - [DatabaseConfiguration](/reference/android/arch/persistence/room/DatabaseConfiguration)\n - [InvalidationTracker](/reference/android/arch/persistence/room/InvalidationTracker)\n - [InvalidationTracker.Observer](/reference/android/arch/persistence/room/InvalidationTracker.Observer)\n - [Room](/reference/android/arch/persistence/room/Room)\n - [RoomDatabase](/reference/android/arch/persistence/room/RoomDatabase)\n - [RoomDatabase.Builder](/reference/android/arch/persistence/room/RoomDatabase.Builder)\n - [RoomDatabase.Callback](/reference/android/arch/persistence/room/RoomDatabase.Callback)\n - [RoomDatabase.MigrationContainer](/reference/android/arch/persistence/room/RoomDatabase.MigrationContainer)\n - [RoomWarnings](/reference/android/arch/persistence/room/RoomWarnings)\n - [RxRoom](/reference/android/arch/persistence/room/RxRoom)\n-\n\n Enums\n -----\n\n - [RoomDatabase.JournalMode](/reference/android/arch/persistence/room/RoomDatabase.JournalMode)\n-\n\n Exceptions\n ----------\n\n - [EmptyResultSetException](/reference/android/arch/persistence/room/EmptyResultSetException)"]]