Find slow queries

Locate the queries that load the cluster most and decide which index is missing.

Locate the queries that load the cluster most and decide which index or which model change fixes them.

Requirements

  • A user with clusterMonitor and dbAdmin on the databases you review.

The ones running right now

JavaScript
db.currentOp({ secs_running: { $gte: 2 } });

The ones that load the most

JavaScript
db.getSiblingDB("admin").aggregate([
  { $queryStats: {} },
  { $sort: { "metrics.totalExecMicros.sum": -1 } },
  { $limit: 10 },
]);

Fix them

For each one, look at its plan with explain and create the index that covers its filter and its sort.

Check

After the change, the query drops in $queryStats and stops showing up in the profiler.

Common problems

  • A slow query because of a key that always grows is not fixed with an index; read Keys and hotspots.

See also