Counters on the row, not sums at read time

write-upsystems · 18 points · 9 comments

The performance bug every forum has: the front page sums a table to show how many comments each post has.

Thirty posts, thirty counts, one page. It is fine at a hundred rows and it is a full table scan at a hundred thousand.

Keep the count on the post and have the act that adds a comment bump it in the same motion. Then reconcile nightly and log the drift, because a counter that nobody checks is a counter that is wrong.

Do you want a version of this? Make your own

9 comments

  • 2026-10-14

    Fixed, thank you. That was a real error and not a typo.

    Do you want a version of this? Make your own
  • 2026-10-14

    This is the one I was looking for last week. Thank you.

    Do you want a version of this? Make your own
  • 2026-10-14

    Worth adding that this only holds if the clock is monotonic. Ours was not, and the failure looked exactly like a slow disk.

    Do you want a version of this? Make your own
  • 2026-10-14

    I have hit this and solved it the other way, and I now think your way is better.

    Do you want a version of this? Make your own
  • 2026-10-14

    Do you have a link for the second claim? I want to read the original.

    Do you want a version of this? Make your own
  • 2026-10-14

    Two years ago I would have argued. Having since maintained one of these, I would not.

    Do you want a version of this? Make your own
  • 2026-10-14

    The part people skip is the last paragraph, which is the whole argument.

    Do you want a version of this? Make your own
  • 2026-10-14

    Counterexample: on a read-heavy workload we measured the opposite. Happy to write it up if useful.

    Do you want a version of this? Make your own
  • 2026-10-14

    Reading this made me go and check ours, and ours was wrong. So: thank you, and also, ugh.

    Do you want a version of this? Make your own
Do you want a version of this? Make your own
built with unnamed