Product

Automating MongoDB Performance Tuning with an AI Database Agent

Part three of our MongoDB performance series: continuous MongoDB performance tuning with Tony, CloudThinker's database agent. Connect read-only in about five minutes, then let the agent watch profiler output, $indexStats, and explain plans as the workload shifts — flagging COLLSCANs, misordered compound indexes, unused indexes, and unbounded array growth the week they appear, not at the next quarterly audit. Every proposal ships with evidence and a staged rollback path, gated by graduated autonomy from Notify to Autonomous. Includes a realistic first-findings table and the chat prompts to try in your first session.

·
mongodbdatabaseperformancemongodbperformanceindextuningaiagentcloudthinkerdevops
Cover Image for Automating MongoDB Performance Tuning with an AI Database Agent

Automating MongoDB Performance Tuning with an AI Database Agent

Every MongoDB index recommendation has an expiry date. The compound index that made your dashboard query instant in January starts limping in June, because a product launch changed the query shape and now the equality field sits behind the sort field. The index review you ran last quarter said "drop these five, they're unused" — but one of them quietly became load-bearing when a new report shipped. Index advice is only correct relative to a workload, and the workload never stops moving.

This is part three of our MongoDB performance series. Part one mapped the five patterns that degrade MongoDB performance — collection scans, wrong compound-index order, unbounded array growth, working sets that outgrow RAM, and write-side drag. Part two walked through auditing all of them with native tools: the profiler, explain("executionStats"), $indexStats, mongotop, and mongostat.

That audit produces a solid picture of your cluster — for the week you ran it. This article covers the continuous version: how Tony, CloudThinker's database agent, keeps that audit running against the live workload, what its first pass on a typical deployment surfaces, and what it refuses to do without your explicit approval.

Connecting Tony: read-only, about five minutes

Tony connects to MongoDB the way you would want any third party to: with a dedicated user holding read-only roles — read on the databases you scope it to, plus clusterMonitor so it can see serverStatus, $indexStats, and profiler output. No driver changes, no agent on your servers, no write access at connection time. You create the user, paste the connection string, and the first scan starts. The whole flow takes about five minutes; the MongoDB connection guide lists the exact roles so your security review can verify what is granted and what is not.

Works the same whether you run a self-managed replica set or Atlas — Tony reads the database's own instrumentation, not a vendor API.

What Tony watches while you do other things

Tony's checks are the part-two audit, re-run continuously instead of the afternoon you could spare for it:

  • Profiler output, aggregated over time. Instead of eyeballing system.profile for one bad hour, Tony clusters slow operations by query shape and tracks each shape's frequency, docsExamined-to-nReturned ratio, and latency trend. A query that regressed 40% over three weeks is a finding, even if it never crossed your slowms threshold on any single day.
  • Explain plans on the shapes that matter. For the top recurring shapes, Tony inspects the winning plan: COLLSCAN stages, in-memory SORT stages, and indexes being used for only a prefix of the predicate.
  • Index health via $indexStats. Indexes with zero or near-zero accesses across a full business cycle, redundant prefixes (a { userId: 1 } index shadowed by { userId: 1, createdAt: -1 }), and the write amplification and cache footprint they cost.
  • Schema shape drift. Document size percentiles per collection, flagging the unbounded-array pattern from part one before a hot document approaches the 16 MB cap — the failure mode that never shows up in an index review because it is a modeling problem, not an index problem.
  • Server vitals from serverStatus. WiredTiger cache eviction pressure, dirty-cache percentage, and connection churn — the signals that tell you whether slow queries are a query problem or a working-set problem.

The value is cadence, not cleverness. The compound index that goes stale in June is flagged in June, with the before-and-after explain plans attached, not discovered in the Q3 audit.

Proposals come with evidence and a rollback path

When Tony proposes a change, it looks like the case a careful DBA would build:

  • Index proposals follow the ESR rule — equality fields first, then sort, then range — and cite the query shapes the index serves, their frequency, and the projected plan change.
  • Drop proposals are staged, not abrupt. For an unused index, Tony proposes hiding it first (hideIndex()), watching for regressions over an observation window you set, and only then dropping. Hiding is instantly reversible; a dropped 40 GB index is a long rebuild.
  • Schema-shape findings are advisory. Tony will show you that the events array in your sessions collection grows without bound and sketch the bucketing alternative — but restructuring documents is an engineering decision it will not make for you.

What Tony may actually execute is governed by graduated autonomy, set per environment and per action class:

  • Notify — report the finding. The default for everything.
  • Suggest — propose the specific command with projected impact and rollback notes.
  • Approve — prepare the action, execute only after a named human approves.
  • Autonomous — execute and report. Typically reserved for reversible actions in non-production — hiding a provably unused index in staging — after the agent has earned trust at the Approve level.

Every finding, proposal, approval, and execution lands in an audit trail with its evidence. Nothing changes in your cluster unless the autonomy level you configured allows it.

What a first pass typically surfaces

Illustrative numbers — a composite of first scans on a mid-market deployment: three-node replica set, ~400 GB data, mixed OLTP and reporting. Yours will differ.

Finding Detail Typical impact
COLLSCAN on hot path orders lookup by customer + status; ~38M docs examined daily p95 drops from roughly 800 ms to under 20 ms
Compound index misordered { createdAt: 1, tenantId: 1 } serving an equality-on-tenant + sort query Removes in-memory SORT stage; steadier p99
Unused indexes 11 indexes, zero accesses in 45 days, ~8 GB combined Less write amplification, ~8 GB of cache back
Redundant index prefix { userId: 1 } shadowed by { userId: 1, createdAt: -1 } One fewer index maintained per write
Unbounded array growth sessions.events; p99 document 4.6 MB and climbing Averts 16 MB-cap failures; smaller cache footprint
Cache eviction pressure Application threads doing eviction at daily reporting peak Explains the "everything is slow at 9am" tickets

Note the shape of the list: two of the six findings (the misordered compound index and the shadowed prefix) are things a point-in-time audit plausibly created — correct decisions that aged out. That is the argument for watching continuously rather than auditing quarterly.

Prompts to try in your first session

Tony is conversational — you ask in plain language and get answers grounded in profiler and $indexStats evidence you can verify yourself:

"Tony, which query shapes consumed the most cumulative execution time this week, and which of them are doing collection scans?"

"Tony, list every index with zero reads in the last 30 days, what each costs in RAM and write overhead, and a staged hide-then-drop plan."

"Tony, propose an index for the slow orders query and show me the explain output before and after."

What Tony will not do

The questions you should ask any tool with a connection string to production, answered plainly:

  • It is read-only by default. The connection user has no write roles. Executing anything requires you to grant scoped permissions and raise the autonomy level above Notify for that action class.
  • It does not build or drop indexes without approval unless you have deliberately set that action to Autonomous for that environment — and drops are staged through hidden indexes first.
  • It does not rewrite your queries or restructure your documents. Schema-shape findings arrive as evidence and options; the modeling decision stays with your team.
  • It does not act outside the audit trail. Every proposal and execution is logged with its evidence and its approver.

The right mental model: Tony is your part-two audit running every day, with a proposal queue attached and a human approval gate in front of anything that changes the cluster.

From snapshot to standing watch

You know what bad looks like in MongoDB, and you know how to find it with the profiler and explain. The gap is that the workload shifts weekly and your audit calendar does not. Teams that move from quarterly index reviews to continuous, approval-gated tuning typically stop the two expensive failure modes cold: the COLLSCAN that ships silently with a new feature, and the "safe to drop" index that no longer is.

Try CloudThinker free — 100 premium credits, no card required — then follow the MongoDB connection guide and see your own first-findings table within the hour.