Turning Learners Into Developers
Codekilla
CODEKILLA
Hard
SQL-O10sql

Denormalise A Hot Counter

Problem

`SELECT COUNT(*) FROM likes WHERE post_id = ?` is now hot and slow at 100M rows. The post page hits it on every render. Propose a denormalisation strategy and write the trigger / atomic SQL that maintains it.

Input
Conceptual + SQL.
Output
Counter table + trigger
Constraints
—
Sample input
—
Sample output
Maintain `posts.likes_count` directly via a trigger or atomic UPDATE on insert/delete.
Explanation
Reads outweigh writes → store the count.
optimisationdenormalisationcounters@Twitter
Visible test cases
in: explain
out: ok
Your solution — run it, use AI if stuck
sql