db optimization - have a total field or query table?

Posted by Dorian Fife on Stack Overflow See other posts from Stack Overflow or by Dorian Fife
Published on 2010-12-25T07:54:45Z Indexed on 2010/12/25 8:54 UTC
Read the original article Hit count: 173

I have an app where users get points for actions they perform - either 1 point for an easy action or 2 for a difficult one. I wish to display to the user the total number of points he got in my app and the points obtained this week (since Monday at midnight).

I have a table that records all actions, along with their time and number of points.

I have two alternatives and I'm not sure which is better:

  1. Every time the user sees the report perform a query and sum the points the user got
  2. Add two fields to each user that records the number of points obtained so far (total and weekly). The weekly points value will be set to 0 every Monday at midnight.

The first option is easier, but I'm afraid that as I'll get many users and actions queries will take a long time.

The second option bares the risk of inconsistency between the table of actions and the summary values.

I'm very interested in what you think is the best alternative here.

Thanks, Dorian

© Stack Overflow or respective owner

Related posts about database-design

Related posts about running-total