Richa Shah
Richa Shah

Reputation: 982

Room database: What is Index specific columns (indices and @Index) and how to use it?

I was referring Index specific columns of Room Database.

Below is some example code is written here https://developer.android.com/training/data-storage/room/defining-data#column-indexing

Example Code-1:

    @Entity(indices = {@Index("name"),
        @Index(value = {"last_name", "address"})})
public class User {
    @PrimaryKey
    public int id;

    public String firstName;
    public String address;

    @ColumnInfo(name = "last_name")
    public String lastName;

    @Ignore
    Bitmap picture;
}

Example Code-2:

@Entity(indices = {@Index(value = {"first_name", "last_name"},
        unique = true)})
public class User {
    @PrimaryKey
    public int id;

    @ColumnInfo(name = "first_name")
    public String firstName;

    @ColumnInfo(name = "last_name")
    public String lastName;

    @Ignore
    Bitmap picture;
}

This is described in room documentation by android, I have used indices for the uniqueness of column but what does above code means can anyone explain it?

Q1: What is the use of indices and @Index?
Q2: What is the difference between @Index("name") and @Index(value = {"last_name", "address"})?

Upvotes: 12

Views: 19421

Answers (3)

David Aleksanyan
David Aleksanyan

Reputation: 3359

In Kotlin actually the syntax is a bit different:

@Entity(indices = [Index(value = ["last_name", "address"])])
data class User(
    @PrimaryKey val id: Int,
    val firstName: String?,
    val address: String?,
    @ColumnInfo(name = "last_name") val lastName: String?,
    @Ignore val picture: Bitmap?
)

This is the Room Reference document with this and lots of other info: RoomDb Reference

Upvotes: 5

Bilal
Bilal

Reputation: 1054

Bit late on the answer. but hope so it will help someone.

Q1: What is the use of indices and @Index?

Indices: Can contain the list of indices on the table. whereas @Index is used to define an index.

For example:

@Entity(indices = {
    @Index("FirstIndex"),@Index(value="last_name",unique = true),
    @Index("SecondIndex"),@Index(value="first_name", unique = true)
})

As you can see, i have defined two indexes in this example which are separated by coma. Here "FirstIndex" and "SecondIndex" are the name of indexes. If i don't define the name of indexes (as you have quote in your example) then 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".

Q2: What is the difference between @Index("name") and @Index(value = {"last_name", "address"})?

@index("name") //syntax to give name of index.
@index(value=last_name", "address") //Syntax to define, which column(s) need to be indexed

Upvotes: 15

Vignesh R
Vignesh R

Reputation: 135

Indexing is the process of adding indexes which are used to quickly locate data without having to search every row in a database table every time the database table is queried or accessed. Room supports indexing certain fields or indexing group of fields using indices property to speed up the queries. you can check the below link for further knowledge.

https://proandroiddev.com/exploring-room-architecture-component-the-extras-cf3f0259ceed

Upvotes: 3

Related Questions