Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 201537
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 11, 20262026-05-11T17:12:28+00:00 2026-05-11T17:12:28+00:00

I’m creating a game with points for doing little things, so I have a

  • 0

I’m creating a game with points for doing little things, so I have a schema as such:

create table points (
  id int,
  points int,
  reason varchar(10)
)

and to get the number of points a user has is trivial:

select sum(points) as total from points where id = ?

however, performance has become more and more of an issue as the points table expand. I want to do something like:

create table pointtotal (
  id int,
  totalpoints int
)

what is the best practice for keeping them in sync? Do I try to update pointtotal on every change? Do I run a daily script?

(Assume I have the right keys – they were left out for conciseness)

Edit:

Here are some characteristics that I left out but should be helpful:

Inserts/Updates to Points are not all that frequent
There are a large number of entries, and there are a large number of requests – the keys were pretty trivial, as you can see.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-11T17:12:28+00:00Added an answer on May 11, 2026 at 5:12 pm

    The best practice is to use a normalized database schema. Then the DBMS keeps it up to date, so you don’t have to.

    But I understand the tradeoff that makes a denormalized design attractive. In that case, the best practice is to update the total on every change. Investigate triggers. The advantage of this practice is that you can make the total keep in sync with the changes so you never have to think about whether it’s out of date or not. If one change is committed, then the updated total is committed too.

    However, this has some weaknesses with respect to concurrent changes. If you need to accommodate concurrent changes to the same totals, and you can tolerate the totals being “eventually consistent,” then use periodic recalculation of the total, so you can be sure only one process at a time is changing the total.

    Another good practice is to cache aggregate totals outside the database, e.g. memcached or in application variables, so you don’t have to hit the database every time you need to display the value.


    The query “select sum(points) as total from points where id = ?” should not take 2 seconds, even if you have a huge number of rows and a lot of requests.

    If you have a covering index defined over (id, points) then the query can produce the result without reading data from the table at all; it can calculate the total by reading values from the index itself. Use EXPLAIN to analyze your query and look for the “Using index” note in the Extra column.

    CREATE TABLE Points (
      id     INT,
      points INT,
      reason VARCHAR(10),
      KEY    id (id,points)
    );
    
    EXPLAIN SELECT SUM(points) AS total FROM Points WHERE id = 1;
    
    +----+-------------+--------+------+---------------+------+---------+-------+------+--------------------------+
    | id | select_type | table  | type | possible_keys | key  | key_len | ref   | rows | Extra                    |
    +----+-------------+--------+------+---------------+------+---------+-------+------+--------------------------+
    |  1 | SIMPLE      | points | ref  | id            | id   | 5       | const |    9 | Using where; Using index | 
    +----+-------------+--------+------+---------------+------+---------+-------+------+--------------------------+
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
this is what i have right now Drawing an RSS feed into the php,
I have just tried to save a simple *.rtf file with some websites and
I have a French site that I want to parse, but am running into
Seemingly simple, but I cannot find anything relevant on the web. What is the
Does anyone know how can I replace this 2 symbol below from the string
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I want to count how many characters a certain string has in PHP, but
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.