简体   繁体   中英

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. 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.

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.

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:

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

movie_tb has 100,000 row.

there is user_tb:

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

user_tb has 10,000 row.

here is 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
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.

this table(commnet_tb) have 31 million rows;
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.

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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