1

I am working on 5 star rating which is working fine. Now I want to optimize the database.

CREATE TABLE IF NOT EXISTS `rating` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `article_id` int(11) NOT NULL,
  `vote` float NOT NULL,
  'user_id' int(11) NOT NULL

  PRIMARY KEY (`id`)
)

This is my current database. Now if 10,000 user rate the same article, I got 10000 records in a single table.

Can anybody helps me to understand and optimize the database?

A J
  • 3,970
  • 14
  • 38
  • 53
Ankita Kashyap
  • 517
  • 6
  • 20

1 Answers1

1

This depends on what your rating algorithm and requirements are. You can just record the number of stars received using five fields instead of one:

star1    integer not null default 0,
star2    integer not null default 0,
...
star5    integer not null default 0,

but if you do, you lose the possibility of "cooling off" ratings and changing idea on the user's part.

On the other hand, this accepted answer algorithm requires only to store the mean, so you can store

stars    integer not null default 0,
voters   integer not null default 0,

...but now the user cannot retract his vote, and indeed not even know whether he has voted or not.

(You can compromise by storing daily or weekly votes into one table, with datetime and voter id and whatever, and then "distilling" older results in the average table. Now the voter can see and undo what he did in the last week).

So what you need to do is to clearly state your requirements - what can the user do? What must the system be able to do? etc. - and work out what information needs to be known; then you derive the table(s) structure from there. If the data is too much (10,000,000 records isn't much) you either try throwing more hardware at it, or see if you can scale somehow - for example if you don't (often) need cross-article information (such as "the most starred articles"), nothing stops you from partitioning the rating table across different servers based on article id until each partition is small enough for your taste.

Community
  • 1
  • 1
LSerni
  • 55,617
  • 10
  • 65
  • 107