How a Simple MongoDB Query Tweak Saved Us Money and Boosted App Performance (250x improvement)

This weekend, I was reviewing our app’s query analytics and noticed something odd: a single query was taking up to 15 seconds to complete in some cases. Most of the time, it ran between 200ms and 2s, which was aweful.

At first glance, the query looked fine. There were no obvious issues. But when I dove deeper into the MongoDB Atlas Search documentation, I discovered something interesting: the type I was using, wasn’t the most efficient choice for the operation.

I was checking for equality, but the string type is optimized for text search, not exact matches. What I should’ve been using was the token type. After updating the search index to use the token type for that field, the query ran faster, around 800ms which was better but still not good.

Encouraged by that result, I went a step further and added a filter to limit the results to the last 6 months. That single change led to another a good performance, around 60ms response times.

Just by tweaking one query, we dramatically improved speed and reduced compute time, saving on performance and cost.

Takeaway

If you’re using MongoDB Atlas, check your query profiler regularly. Identify your long running and frequently used queries, and explore ways to optimize them. Sometimes, it’s not about rewriting the whole thing, just using the right type or adding a filter can make all the difference.

Deja un comentario

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *