[英]How to optimize query performance in a many to many table relationship
I have a user table and a virtual item table, each with ID values as primary keys.我有一个用户表和一个虚拟项目表,每个表都以 ID 值作为主键。
I also have a favorites table where users can store their favorite items.我还有一个收藏夹表,用户可以在其中存储他们最喜欢的项目。 The favorites table maps user_id to item_id.
收藏夹表将 user_id 映射到 item_id。 Many users can favorite many items.
许多用户可以喜欢许多项目。
table users
user_id bigint primary key auto_increment
...
table items
item_id bigint primary key auto_increment
...
table favorites
user_id bigint
item_id bigint
Since I cannot create indexes on the favorites table, because there are no unique or primary keys, how do i optimize search queries, such as SELECT?由于我无法在收藏夹表上创建索引,因为没有唯一键或主键,我该如何优化搜索查询,例如 SELECT? Is there a better way to store the data?
有没有更好的方法来存储数据?
Since I cannot create indexes on the favorites table, because there are no unique or primary keys
由于我无法在收藏夹表上创建索引,因为没有唯一键或主键
Huh?嗯? Of course you can create indexes.
当然,您可以创建索引。 In fact, two come to mind:
其实想到两个:
favorites(user_id, item_id)
favorites(item_id, user_id)
These would be used to answer different questions.这些将用于回答不同的问题。 Respective examples are: What are the favorites of user X?
各自的例子是:用户 X 最喜欢什么? What users have a favorite of Y?
哪些用户最喜欢 Y?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.