Room 数据库:什么是索引特定列(索引和@Index)以及如何使用它?
Room database: What is Index specific columns (indices and @Index) and how to use it?
我指的是 Room 数据库的索引特定列。
下面是一些示例代码写在这里 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:indices和@Index有什么用?
Q2:@Index("name")
和@Index(value = {"last_name", "address"})
有什么区别?
索引是添加索引的过程,索引用于快速定位数据,而无需在每次查询或访问数据库 table 时搜索数据库中的每一行 table。
Room 支持使用索引 属性 索引某些字段或索引字段组以加快查询速度。您可以查看下面的内容 link 以了解更多信息。
https://proandroiddev.com/exploring-room-architecture-component-the-extras-cf3f0259ceed
回答有点晚了。但希望它能对某人有所帮助。
Q1:indices
和@Index
有什么用?
Indices
: 可以包含 table 上的索引列表。而 @Index
用于定义 index.
例如:
@Entity(indices = {
@Index("FirstIndex"),@Index(value="last_name",unique = true),
@Index("SecondIndex"),@Index(value="first_name", unique = true)
})
如你所见,我在这个例子中定义了两个索引,用逗号分隔。这里“FirstIndex”和“SecondIndex”是索引的名称。如果我没有定义索引的名称(正如您在示例中引用的那样),那么 Room 会将其设置为由“”连接并以“index${ table名称}”。因此,如果您有一个名为“Foo”且索引为 {“bar”、“baz”} 的 table,生成的索引名称将为“index_Foo_bar_baz”。
Q2:@Index("name")
和@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
在Kotlin中实际上语法有点不同:
@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?
)
这是房间参考文档,其中包含此信息和许多其他信息:RoomDb Reference
我指的是 Room 数据库的索引特定列。
下面是一些示例代码写在这里 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:indices和@Index有什么用?
Q2:@Index("name")
和@Index(value = {"last_name", "address"})
有什么区别?
索引是添加索引的过程,索引用于快速定位数据,而无需在每次查询或访问数据库 table 时搜索数据库中的每一行 table。 Room 支持使用索引 属性 索引某些字段或索引字段组以加快查询速度。您可以查看下面的内容 link 以了解更多信息。
https://proandroiddev.com/exploring-room-architecture-component-the-extras-cf3f0259ceed
回答有点晚了。但希望它能对某人有所帮助。
Q1:indices
和@Index
有什么用?
Indices
: 可以包含 table 上的索引列表。而 @Index
用于定义 index.
例如:
@Entity(indices = {
@Index("FirstIndex"),@Index(value="last_name",unique = true),
@Index("SecondIndex"),@Index(value="first_name", unique = true)
})
如你所见,我在这个例子中定义了两个索引,用逗号分隔。这里“FirstIndex”和“SecondIndex”是索引的名称。如果我没有定义索引的名称(正如您在示例中引用的那样),那么 Room 会将其设置为由“”连接并以“index${ table名称}”。因此,如果您有一个名为“Foo”且索引为 {“bar”、“baz”} 的 table,生成的索引名称将为“index_Foo_bar_baz”。
Q2:@Index("name")
和@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
在Kotlin中实际上语法有点不同:
@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?
)
这是房间参考文档,其中包含此信息和许多其他信息:RoomDb Reference