电影的数据库设计关系

Database design relationship for movie

我在 MySQL 中创建了一个 table 的电影,其中有一百万部电影,例如带有 id 和 name 列。我还创建了用户 table,其中包含 ID、名称列和评论 table,其中包含 movie_id 和 user_id 以及评论列以保存所有用户评论。

我的问题是如果我有 100 万部电影,如果每部电影有 1000 条评论(假设我们有 1000 个活跃用户),那么评论 table 应该有 1,000,000,000 行而且使用起来很慢查找对一部特定电影的所有评论的查询。

你们中的任何朋友可以帮助我提供更好的解决方案或方法吗?

这是我用来创建我项目的 table 的查询:

movie_tb:

create table movie_tb (id bigint not null primary key auto_increment,movie_name   varchar(30) not null);  

user_table:

create table user_tb (id bigint not null primary key auto_increment,user_name   varchar(30) not null);  

comment_tb:

create table comment_tb(id bigint not null primary key auto_increment,movie_id   bigint not null,user_id bigint not null,comment varchar(250));  

movie_table 看起来像这样:

+---------+--------------+  
|  id     |  movie_name  |  
+---------+--------------+  
|  1      |  Joker       |  
+---------+--------------+  
+---------+--------------+  
|  2      |  Avatar      |  
+---------+--------------+  
+---------+--------------+  
|  3      | Harry potter |  
+---------+--------------+  

movie_tb 有 100,000 行。

有user_tb:

+---------+--------------+  
|id       |   name       |  
+---------+--------------+  
+---------+--------------+  
|  1      | jack         |  
+---------+--------------+  
+---------+--------------+  
|    2    |     joy      |  
+---------+--------------+  

user_tb 有 10,000 行。

这里是comment_tb:

+---------+--------------+---------------+---------------+  
|  id     | movie_id     | user_id       | comment       |  
+---------+--------------+---------------+---------------+  
+---------+--------------+---------------+---------------+  
|  1      | 1            | 2             | comment1      |  
+---------+--------------+---------------+---------------+  
+---------+--------------+---------------+---------------+  
|  2      | 1            | 3             | comment2      |  
+---------+--------------+---------------+---------------+  
+---------+--------------+---------------+---------------+  
|  3      | 1            | 5             | comment3      |  
+---------+--------------+---------------+---------------+  
+---------+--------------+---------------+---------------+  
|  4      | 1            | 1024          | comment4      |  
+---------+--------------+---------------+---------------+  
+---------+--------------+---------------+---------------+  
|  5      | 1475         | 505           | comment       |  
+---------+--------------+---------------+---------------+  
+---------+--------------+---------------+---------------+  
|  6      | 1475         | 56            | comment       |  
+---------+--------------+---------------+---------------+  
+---------+--------------+---------------+---------------+  
|  7      | 2            | 2             | comment       |  
+---------+--------------+---------------+---------------+  
+---------+--------------+---------------+---------------+  
|  8      | 1            | 8761          | comment5      |   
+---------+--------------+---------------+---------------+  
+---------+--------------+---------------+---------------+  
|  ...    | ...          |  ...          |   ...         |   
+---------+--------------+---------------+---------------+  
+---------+--------------+---------------+---------------+  
|31000000 | 1            | 36            | comment6      | 
+---------+--------------+---------------+---------------+  
  

这里有一小行comment_tb: 小丑有6条评论(movei_id=1
参考 movie_tb 中的电影 ID 和评论来自 user_id 2,3,5,1024,8761,36 参考 user_tb.

中的用户 ID

这个table(commnet_tb)有3100万行;
我使用这个查询:

select comment from comment_tb where movie_id= 1;  

获取关于 movie_id 1 的所有评论,这是百搭,但它在 8 秒或有时 30 秒后得到结果。

如果您有十亿条评论,那么使用关系数据库的解决方案需要十亿行评论 table。

另一方面,如果您正确设计数据库,这不会减慢查询速度:只需在注释 table 的 user_id 和 movie_id 列上添加索引,并且查找某部电影的所有评论或某个用户​​的所有评论的查询将得到非常有效的回答,而无需“扫描”所有 table.

如果 MySQL 是您必须存储评论的唯一选择,那么根据您的用例,您还可以考虑对用户评论数据进行分区。