# Index

Artifact: [androidx.room3:room3-common](https://developer.android.com/jetpack/androidx/releases/room3.md.txt) [View Source](https://cs.android.com/search?q=file:androidx/room3/Index.kt+class:androidx.room3.Index) Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/room3.md.txt#3.0.0)

*** ** * ** ***

Common/AllAndroid/JVMNative/C/iOSJavaScript
- Cmn

  ```
  @Target(allowedTargets = )
  @Retention(value = AnnotationRetention.BINARY)
  annotation Index
  ```

*** ** * ** ***

Declares an index on an `Entity`.

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 it.

There are two ways to define an index in an `Entity`. You can either set `ColumnInfo.index` property to index individual properties or define composite indices via `Entity.indices`.

If an indexed property 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 `Entity.inheritSuperIndices` to `true`.

See also [SQLite Index Documentation](https://sqlite.org/lang_createindex.html)

## Summary

| ### Nested types |
|---|
| `enum Index.Order : Enum<Index.Order>` |

| ### Public constructors |
|---|---|
| `Index( vararg value: String, orders: Array<Index.Order>, name: String, unique: Boolean )` | Cmn |

| ### Public properties |
|---|---|---|
| `String` | `name` Name of the index. | Cmn |
| `Array<Index.Order>` | `orders` List of column sort orders in the `Index`. | Cmn |
| `Boolean` | `unique` If set to true, this will be a unique index and any duplicates will be rejected. | Cmn |
| `Array<String>` | `value` List of column names in the Index. | Cmn |

## Public constructors

### Index

Cmn Added in [3.0.0](https://developer.android.com/jetpack/androidx/releases/room3.md.txt#3.0.0)

```
Index(
    vararg value: String,
    orders: Array<Index.Order> = [],
    name: String = "",
    unique: Boolean = false
)
```

## Public properties

### name

Cmn

```
val name: String
```

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" and "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 via this annotation value.

| Returns |
|---|---|
| `String` | The name of the index. |

### orders

Cmn

```
val orders: Array<Index.Order>
```

List of column sort orders in the `Index`.

The number of entries in the array should be equal to size of columns in `value`.

The default order of all columns in the index is `Order.ASC`.

Note that there is no value in providing a sort order on a single-column index. Column sort order of an index are relevant on multi-column indices and specifically in those that are considered 'covering indices', for such indices specifying an order can have performance improvements on queries containing `ORDER BY` clauses. See [SQLite documentation](https://www.sqlite.org/queryplanner.html#_sorting_by_index) for details on sorting by index and the usage of the sort order by the query optimizer.

As an example, consider a table called 'Song' with two columns, 'name' and 'length'. If a covering index is created for it: `CREATE INDEX song_name_length on Song (name ASC, length DESC)`, then a query containing an `ORDER BY` clause with matching order of the index will be able to avoid a table scan by using the index, but a mismatch in order won't. Therefore, the columns order of the index should be the same as the most frequently executed query with sort order.

| Returns |
|---|---|
| `Array<Index.Order>` | The list of column sort orders in the Index. |

### unique

Cmn

```
val unique: Boolean
```

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

Cmn

```
val value: Array<String>
```

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](https://www.sqlite.org/optoverview.html) for details on index usage in the query optimizer.

| Returns |
|---|---|
| `Array<String>` | The list of column names in the `Index`. |