Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Since ratings are fairly static and your dataset is large, you could cache (denormalize) the data in the movie record when a user rates a movie. You need to keep track of the number of both votes and the sum of the votes, so the average is accurate. Your movie rating is dynamically calculated when you need it. Here's the schema:</p> <pre><code>create table movie as ( movie_id int not null primary key, -- your current columns vote_count int, vote_sum int ); </code></pre> <p>then use a view to help out</p> <pre><code>create view movie_view as select *, vote_sum/vote_count as vote_average from movie; </code></pre> <p>assuming you have a table that looks like this:</p> <pre><code>create table user_movie_vote ( user_id int references user, movie_id int references movie, vote int ); </code></pre> <p>you could use a trigger to keep the vote totals up to date for you:</p> <pre><code>delimiter ~ create trigger movie_vote_trg after insert on user_movie_vote for each row begin update movie set vote_count = vote_count + 1, vote_sum = vote_sum + new.vote where movie_id = new.movie_id; end~ delimiter ; </code></pre> <p>if votes can be updated, you'll need:</p> <pre><code>delimiter ~ create trigger movie_vote_trg after update on user_movie_vote for each row begin update movie set vote_sum = vote_sum + new.vote - old.vote where movie_id = new.movie_id; end~ delimiter ; </code></pre> <p>if votes can be deleted, you'll need:</p> <pre><code>delimiter ~ create trigger movie_vote_trg after delete on user_movie_vote for each row begin update movie set vote_sum = vote_sum - old.vote, vote_count = vote_count - 1 where movie_id = new.movie_id; end~ delimiter ; </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload