How I Cut Database Latency with the Right Index
An ecommerce case where the right index and a restructured query transformed the performance of the app.
The product listing of an online store took several seconds to load. The problem was not the infrastructure: it was the database. A query with unnecessary joins, no index on the filtering column and a subquery that ran once per row.
The first change was adding a composite index on the category and price columns, which were the ones the client used to filter and sort. That change alone already cut the load time noticeably.
The second was rewriting the query: removing the correlated subquery, turning the joins into a single query with aggregation and paginating in the database instead of pulling everything into memory. The listing went on to load almost instantly, without changing a single server.
