MySQL - Calculating fields on the fly vs storing calculated data

Posted by Christian Varga on Stack Overflow See other posts from Stack Overflow or by Christian Varga
Published on 2010-12-22T11:28:25Z Indexed on 2010/12/22 12:54 UTC
Read the original article Hit count: 244

Filed under:
|
|
|

Hi Everyone,

I apologise if this has been asked before, but I can't seem to find an answer to a question that I have about calculating on the fly vs storing fields in a database.

I read a few articles that suggested it was preferable to calculate when you can, but I would just like to know if that still applies to the following 2 examples.

Example 1. Say you are storing data relating to a car. You store the fuel tank size in litres, and how many litres it uses per 100km. You also want to know how many KMs it can travel, which can be calculated from the tank size and economy. I see 2 ways of doing this:

  1. When a car is added or updated, calculate the amount of KMs and store this as a static field in the database.
  2. Every time a car is accessed, calculate the amount of KMs on the fly.

Because the cars economy/tank size doesn't change (although it could be edited), the KMs is a pretty static value. I don't see why we would calculate it every single time the car is accessed. Wouldn't this waste cpu time as opposed to simply storing it in a separate field in the database and calculating only when a car is added or updated?

My next example, which is almost an entirely different question (but on the same topic), relates to counting children.

Let's say we have a app which has categories and items. We have a view where we display all the categories, and a count of all the items inside each category. Again, I'm wondering what's better. To perform a MySQL query to count all the items in each category every single time the page is accessed? Or store the count in a field in the categories table and update when an item is added / deleted?

I know it is redundant to store anything that can be calculated, but I worry that calculating fields or counting records might be slow as opposed to storing the data in a field. If it's not then please let me know, I just want to learn about when to use either method. On a small scale I guess it wouldn't matter either way, but apps like Facebook, would they really count the amount of friends you have every time someone views your profile or would they just store it as a field?

I'd appreciate any responses to both of these scenarios, and any resource that might explain the benefits of calculating vs storing.

Thanks in advance,

Christian

© Stack Overflow or respective owner

Related posts about php

Related posts about mysql