Search code examples
databasescoring

How do you pre-compute a scoring system from the database?


Stackoverflow displays your reputation next to your username. I suppose it doesn't hit the database to calculate your score on every page render (or perhaps it does?). Would there be a total score field in the users table that gets updated when someone upvotes me?

What is the best practice for these kind of systems? I'm sorry if it's an obvious question, I'm new to databases.


Solution

  • When you log in, the number is pulled from the database along with all your users information.

    It can be a user object. That user object has a property score which is initialized to whatever that number is in the database when you just log in.

    As you go around the website and do stuff, the property is updated but not the database.

    You can then update the database only once when the object user is destructed (either by logout or closing the browser) or after a certain amount of time.