【发布时间】:2019-07-20 00:58:03
【问题描述】:
我指的是房间数据库的索引特定列。
下面是一些示例代码写在这里https://developer.android.com/training/data-storage/room/defining-data#column-indexing
示例代码 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;
}
示例代码 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;
}
这在 android 的房间文档中有所描述, 我已经使用索引来表示列的唯一性,但是上面的代码意味着什么,谁能解释一下?
Q1:索引和@Index有什么用?
Q2:@Index("name")和@Index(value = {"last_name", "address"})有什么区别?
【问题讨论】:
-
索引不是为了唯一性。这是为了在给定的列上更快地搜索。如何生成唯一值可以在这里找到:stackoverflow.com/a/44109830/1860868
-
name上的索引是一列,{"first_name", "last_name"}上的索引是跨多列的复合索引。 -
你能详细解释一下吗?
标签: android android-room android-architecture-components