← Journal
12 September 2025 · 6 min read

How I Cut Event Load Times from 8 Seconds to 1 Second

Backend DevelopmentDatabase OptimizationMongoDB

By tracing a slow page all the way to the database, and refusing to stop at "good enough."

Introduction

During my internship at DevRelSquad (now part of GoAvo.ai), I worked on the performance of an events listing page that was noticeably slow under real-world usage.

The endpoint powering it took 6 to 8 seconds to respond to what should have been a straightforward paginated query. With competing priorities in a fast-moving team, nobody had dug into it deeply yet.

I decided to trace it end to end, from the browser to the database, to find exactly where the time was going.

The Symptom

The endpoint handled filtering, pagination, and scoping by status, ownership, and date. On paper, routine. In practice, response times were inconsistent and slow enough to hurt usability.

The delay clearly came from the API rather than the frontend, so I focused on the backend path.

Step 1: Isolating the Bottleneck

I added timing instrumentation around the database call to break down the request lifecycle. The answer was immediate: almost all of the 6 to 8 seconds was spent inside the database query. Application logic and serialization contributed almost nothing.

That narrowed the problem space considerably.

Step 2: Understanding the Data Access Pattern

Digging into how the endpoint decided which events to show, I found the real issue, and it wasn't just a query problem. It was a schema design problem.

Event status wasn't stored on documents. It was derived at runtime by comparing timestamps:

  • End date in the past means completed.
  • Start date in the future means upcoming.
  • Status was calculated in memory, after fetching documents.

Because status wasn't stored, it couldn't be indexed. And without an index, every query was a full collection scan: MongoDB read through the entire collection on every request.

# illustrative
events = db.events.find({})
for event in events:
    if event["end_date"] < now:
        event["status"] = "completed"
    elif event["start_date"] > now:
        event["status"] = "upcoming"
flowchart LR
    subgraph Before
        Q1[Request] --> S1[find all events<br/>COLLSCAN] --> M1[Compute status<br/>in memory] --> F1[Filter + paginate<br/>in app]
    end
    subgraph After
        Q2[Request] --> S2[find status + scope<br/>IXSCAN] --> F2[Already filtered<br/>+ paginated]
    end

Step 3: Designing the Fix

The fix had two parts, and both were necessary.

Store status explicitly

Instead of computing status on every read, I introduced a stored field with discrete values: active, draft, and archived.

This moves a small amount of work to write time in exchange for dramatically faster reads. It also makes the data model more honest: status is a real business concept, not a by-product of timestamp arithmetic.

Build indexes around it

With a stored status field, I could create compound indexes shaped by the actual query patterns, leading with status as the most selective field, followed by the ownership and date fields used for scoping and range queries.

Purpose Index fields
Basic filtering status
Tenant scoping status, tenant_id
Ownership filtering status, owner_id
Past events status, end_date (desc)
Upcoming events status, start_date (asc)

MongoDB compound indexes are consumed left to right. Leading with status meant the query planner could narrow to the relevant slice of the collection before touching anything else.

Step 4: Updating the Query Logic

With the new schema and indexes in place, the queries had to be rewritten to use them.

Backward compatibility mattered: existing documents pre-dated the new field. Queries had to handle documents where status was absent, empty, or set, without requiring an immediate full migration.

# illustrative
if requested_status == "active":
    query["$or"] = [
        {"status": {"$exists": False}},
        {"status": ""},
        {"status": "active"},
    ]
else:
    query["status"] = requested_status

I applied this pattern consistently across list queries, single-record fetches, navigation logic, and aggregation pipelines.

Step 5: Measuring the Impact

I validated the change in staging against realistic data volumes, using MongoDB's explain("executionStats") to confirm the actual query plan, not just wall-clock time.

Before After
Response time ~8 s ~1 s
Query plan COLLSCAN IXSCAN
xychart-beta
    title "Events endpoint response time"
    x-axis ["Before (COLLSCAN)", "After (IXSCAN)"]
    y-axis "Seconds" 0 --> 9
    bar [8, 1]

An 87.5% reduction in response time.

I presented the explain() output, the timing comparisons, and the code diff to my seniors, and they approved it for production.

Step 6: Shipping It

Rollout was incremental: indexes were built in the background, the schema handled mixed document states safely, and monitoring confirmed the improvement held in production.

The events page went from feeling broken to feeling instant.

Why It Worked

  • Move computation to write time. Deriving status once on write avoids repeating that work on every read. Read-heavy endpoints feel this trade-off immediately.
  • You can't index a computed value. Making status a real stored field was the prerequisite for everything else. No schema change, no index, no improvement.
  • Lead with your most selective field. Compound indexes are consumed left to right, so status as the leading key let MongoDB discard most of the collection before applying any other condition.
  • Handle schema transitions safely. The $or pattern kept queries correct across mixed documents without a risky upfront migration.
  • explain() is ground truth. COLLSCAN versus IXSCAN tells you definitively what the planner is doing. Don't guess.

One Thing to Watch

Endpoints that sort heavily on _id can occasionally lead the planner to pick a status-based index, which adds an in-memory sort step. At the current scale that isn't a problem, but extending the indexes to include the sort field is the natural next step if it ever becomes one.

The Takeaway

The performance problem wasn't caused by a single bad query. It came from a mismatch between how data was modelled and how it was accessed: status lived in the application layer but had no representation in the database.

By storing it, indexing it, and querying it correctly, the system moved from full collection scans to targeted index lookups.

8 seconds → 1 second. No new infrastructure.

Debugging a slow endpoint? Check the query plan first. A COLLSCAN on a filtered query is often a sign that the data model and indexing strategy need revisiting, not just the query.