Skip to content

Most visited

Recently visited

navigation
added in version 1.0.0
belongs to Maven artifact android.arch.persistence.room:common:1.0.0

Index

public abstract @interface Index
implements Annotation

android.arch.persistence.room.Index


Declares an index on an Entity. see: SQLite Index Documentation

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.

Summary

Public methods

String name()

Name of the index.

boolean unique()

If set to true, this will be a unique index and any duplicates will be rejected.

String[] value()

List of column names in the Index.

Inherited methods

From interface java.lang.annotation.Annotation

Public methods

name

String name ()

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.

Returns
String[] The list of column names in the Index.

This site uses cookies to store your preferences for site-specific language and display options.

Get the latest Android developer news and tips that will help you find success on Google Play.

* Required Fields

Hooray!

Follow Google Developers on WeChat

Browse this site in ?

You requested a page in , but your language preference for this site is .

Would you like to change your language preference and browse this site in ? If you want to change your language preference later, use the language menu at the bottom of each page.

This class requires API level or higher

This doc is hidden because your selected API level for the documentation is . You can change the documentation API level with the selector above the left navigation.

For more information about specifying the API level your app requires, read Supporting Different Platform Versions.

Take a short survey?
Help us improve the Android developer experience.
(Sep 2017 survey)