Indexing Like a Jedi: How I Tamed My Database
The Quest Begins (The "Why") I was building a tiny rate‑limiter for a side‑project API. The idea was simple: every request writes a row with user_id and requested_at (a timestamp) into a rate_limit_log table, and before allowing the request we count how many rows exist for that user in the last minute. If the count exceeds the limit we reject the request. At first it felt like a breeze. I threw…
A developer built a rate limiter for an API by logging each request with user ID and timestamp into a table. Initially, the query ran quickly, but as traffic grew, it took hundreds of milliseconds per request. The culprit was the lack of an index; the simple count scan the entire table, making the operation too slow.
A compound index on `(user_id, requested_at)` was added, turning the query into an index range scan that only checks the relevant rows. This improved performance by 400×, allowing the API to handle traffic bursts without issues. The article explains how indexing works as a sorted map, allowing the database engine to jump directly to the needed rows instead of scanning all of them.
The guide provides tips on pitfalls like over-indexing, forgetting to vacuum, or using functions on indexed columns. It emphasizes that proper indexing turns developers from query writers to query architects who can predict query costs and plan system capacity. The author encourages readers to apply these lessons to improve database performance in their own projects.
Written by urgent.news from Dev.to's reporting — not their text. Machine-written — may contain errors; check the original before relying on it.