As33
@periodic/
arsenic
hot_path
🔴 Critical

Slow query on a frequently hit execution path

This query appears on a hot execution path and contributes significantly to overall request latency. It is a high-priority optimization target because it is both slow AND frequently executed — a compound problem.

Common Causes

  • Missing indexes on frequently queried fields
  • Inefficient query structure executed at high frequency
  • Large dataset without pagination on a popular route
  • Complex joins on unindexed columns in hot paths

How to Fix

  1. 1.Add appropriate indexes for the queried fields
  2. 2.Implement caching (Redis, Memcached) for this result
  3. 3.Optimize the query structure or use projections
  4. 4.Add pagination/limits to bound result size

Compound problem

A query that takes 200ms and runs once a day is fine. The same query on every GET /users/:id call at 500 req/s is not. Frequency multiplies impact.

Example

typescript
// BAD — slow findOne on every GET /users/:id
app.get('/users/:id', async (req, res) => {
  const user = await User.findOne({ _id: req.params.id });
  res.json(user);
});

// GOOD — add index on _id (already indexed) and cache the result
app.get('/users/:id', async (req, res) => {
  const cached = await redis.get(`user:${req.params.id}`);
  if (cached) return res.json(JSON.parse(cached));

  const user = await User.findOne({ _id: ${req.params.id} }).select('-__v');
  await redis.setex(`user:${req.params.id}`, 300, JSON.stringify(user));
  res.json(user);
});

Measuring impact

typescript
// Tag your exporter to track cumulative cost
const monitor = createMonitor({
  exporter: (event) => {
    if (event.signals.includes('hot_path')) {
      metrics.increment('db.hot_path', {
        route: event.requestContext?.path,
        model: event.model,
        durationMs: event.durationMs,
      });
    }
  },
});