简体   繁体   English

电影的数据库设计关系

[英]Database design relationship for movie

I created a table of movie in MySQL that has one million movies for example with id and name column.我在 MySQL 中创建了一个电影表,其中包含一百万部电影,例如带有 id 和 name 列。 I also create user table with id, name columns and a comment table that has movie_id and user_id and comment columns to save all user comments.我还创建了包含 id、名称列的用户表和包含 movie_id 和 user_id 以及评论列的评论表,以保存所有用户评论。

My question is if I have 1 million movies and if each movie has 1000 comments (lets say we have 1000 active users), then the comment table should have 1,000,000,000 rows and it is so slow to use a query to find all comment on one specific movie.我的问题是如果我有 100 万部电影并且如果每部电影有 1000 条评论(假设我们有 1000 个活跃用户),那么评论表应该有 1,000,000,000 行并且使用查询来查找对一个特定的所有评论太慢了电影。

Any of you friends can help me with better solution or way to do this?你们中的任何朋友都可以帮助我提供更好的解决方案或方法吗?

here are the queries that I use to create tables of my project:这是我用来创建项目表的查询:

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 looks like this: movie_table 看起来像这样:

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

movie_tb has 100,000 row. movie_tb 有 100,000 行。

there is user_tb:有 user_tb:

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

user_tb has 10,000 row. user_tb 有 10,000 行。

here is comment_tb:这是 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      | 
+---------+--------------+---------------+---------------+  
  

here is a small of rows inside comment_tb: there are 6 comments on joker(movei_id=1这是 comment_tb 中的一小部分行:关于 joker(movei_id=1) 有 6 条评论
refer to movie id in movie_tb and comment are from user_id 2,3,5,1024,8761,36 that refer to id of users in user_tb.引用 movie_tb 中的电影 id,评论来自 user_id 2,3,5,1024,8761,36 引用 user_tb 中用户的 id。

this table(commnet_tb) have 31 million rows;这个表(commnet_tb)有 3100 万行;
I use this query:我使用这个查询:

select comment from comment_tb where movie_id= 1;  

to get all comments on movie_id 1 which is joker, but it get a result after 8 or some times 30 seconds.获得关于 movie_id 1 的所有评论,它是小丑,但它会在 8 秒或 30 秒后得到结果。

If you have a billion of comments then a solution with a relational database requires a billion of rows for the comments table.如果您有十亿条评论,那么使用关系数据库的解决方案需要评论表有十亿行。

On the other hand this does not slow down queries if you design correctly the database: just add an index on the user_id and movie_id columns of the comments table, and the query that finds all the comments for a certain film, or all the comments of a certain user, will be answered very efficiently, without “scanning” all the table.另一方面,如果您正确设计数据库,这不会减慢查询速度:只需在 comments 表的 user_id 和 movie_id 列上添加索引,然后查询查找某部电影的所有评论,或所有评论某个用户将得到非常有效的回答,而无需“扫描”所有表格。

If MySQL is the only choice you have to store the comments then based on your use case you can also look at partitioning the data for user comments.如果 MySQL 是您必须存储评论的唯一选择,那么根据您的用例,您还可以考虑对数据进行分区以获取用户评论。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM