[英]This might cause a full table scan when resolving the relationship, it is highly advised to create an index that covers this column
I have many-to-many relationship between User and Poll.我在 User 和 Poll 之间存在多对多关系。 While creating the relating class below I'm getting this warning on compile time:
在下面创建相关的 class 时,我在编译时收到此警告:
warning: The column pollId in the junction entity com.example.appproject.model.user.UserPollCrossRef is being used to resolve a relationship but it is not covered by any index.警告:联结实体 com.example.appproject.model.user.UserPollCrossRef 中的列 pollId 正用于解析关系,但它未被任何索引覆盖。 This might cause a full table scan when resolving the relationship, it is highly advised to create an index that covers this column.
这可能会导致在解析关系时进行全表扫描,强烈建议创建一个覆盖该列的索引。
@Entity(primaryKeys = {"uid","pollId"})
public class UserPollCrossRef {
@NonNull
public String uid;
@NonNull
public String pollId;
public UserPollCrossRef(@NonNull String uid, @NonNull String pollId) {
this.uid = uid;
this.pollId = pollId;
}
}
I wanted to ask what does it mean and how can I fix it?我想问一下这是什么意思,我该如何解决?
It looks as though the UserPollCrossRef is an associate/reference/mapping (and other names) table for a many-many relationship between user and poll.看起来 UserPollCrossRef 是一个关联/引用/映射(和其他名称)表,用于用户和轮询之间的多对多关系。
what does it mean
这是什么意思
The primary key is according to uid and pollid combined in that order, thus finding the uid is fast as the uid is the first part of the primary key.主键是按照uid和pollid的顺序组合的,所以uid是主键的第一部分,所以查找uid的速度很快。
However when/if searching for the pollid then there is no index ordered according to the pollid and hence the suggestion that such an index is created.但是,当/如果搜索 pollid 时,则没有根据 pollid 排序的索引,因此建议创建这样的索引。
you may wish to consider looking at https://www.essentialsql.com/what-is-a-database-index/您不妨考虑查看https://www.essentialsql.com/what-is-a-database-index/
Room is simple saying that in some circumstances and index may be beneficial, it is just a warning.房间很简单,说在某些情况下索引可能是有益的,它只是一个警告。
how can I fix it?
我该如何解决?
The simplest way is to use @ColumnInfo(index = true)
eg最简单的方法是使用
@ColumnInfo(index = true)
例如
@Entity(primaryKeys = {"uid","pollId"})
public class UserPollCrossRef {
@NonNull
public String uid;
@NonNull
@ColumnInfo(index = true)
public String pollId;
public UserPollCrossRef(@NonNull String uid, @NonNull String pollId) {
this.uid = uid;
this.pollId = pollId;
}
}
The alternative would be to utilise the indices parameter of the @Entity
annotation eg另一种方法是利用
@Entity
注释的 indices 参数,例如
@Entity(primaryKeys = {"uid","pollId"},
indices = {
@Index(value = "pollId")
}
)
public class UserPollCrossRef {
@NonNull
public String uid;
@NonNull
public String pollId;
public UserPollCrossRef(@NonNull String uid, @NonNull String pollId) {
this.uid = uid;
this.pollId = pollId;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.