---
title: "Finding performance wins in our own traces with a coding agent"
description: "An engineer used a coding agent connected to Sentry MCP to inspect Sentry’s own traces and find backend performance problems. The agent sorted spans by execution frequency and duration, then returned candidate fixes with a file and line, estimated weekly volume, and an example trace. Django query spans included SQL as their description, while query-source attributes supplied file and line and the user_agent attribute exposed a sentry-cli set-commits workload paging through roughly 16k repositories every 40 seconds. For SQL changes, the agent ran EXPLAIN ANALYZE before and after on a read-only database mirror and compared results. Fixes included reusing a loaded project to remove an estimated 143M weekly SELECTs, rewriting a GroupHistory lookup to probe each status separately, and eager-loading Sentry app relations; the GroupHistory query fell from 935ms to 0.4ms with a warm cache."
---

# Finding performance wins in our own traces with a coding agent

[Sentry](https://yomu.fyi/company/sentry) · Scott Cooper · Nov 3, 2026

**Type:** Problem & solution

## Summary

An engineer used a coding agent connected to Sentry MCP to inspect Sentry’s own traces and find backend performance problems. The agent sorted spans by execution frequency and duration, then returned candidate fixes with a file and line, estimated weekly volume, and an example trace. Django query spans included SQL as their description, while query-source attributes supplied file and line and the user\_agent attribute exposed a sentry-cli set-commits workload paging through roughly 16k repositories every 40 seconds. For SQL changes, the agent ran EXPLAIN ANALYZE before and after on a read-only database mirror and compared results. Fixes included reusing a loaded project to remove an estimated 143M weekly SELECTs, rewriting a GroupHistory lookup to probe each status separately, and eager-loading Sentry app relations; the GroupHistory query fell from 935ms to 0.4ms with a warm cache.

## Context

Sentry’s backend is largely Django, and its Django SDK instrumentation emits query spans whose descriptions contain SQL. Traces also include cache calls, cross-silo RPCs, tasks, and API routes. The engineer connected Claude with Opus 5.5 to Sentry MCP and pointed it at Sentry’s own organization and project, starting from a small performance PR and then asking broad questions about critical paths and database-heavy issues. The agent’s candidate list included file and line, estimated weekly volume, and an example trace, and every PR linked a trace showing the extra query or call.

## Approach / What changed

The agent was connected to Sentry MCP and pointed at Sentry’s own organization and project. It sorted spans by how often they ran and how long they took, grouped spans by description, and used query-source attributes to identify file and line. For each candidate it supplied an estimated weekly volume and an example trace. The engineer then had it read the code and git history, assess risk, and validate SQL changes with EXPLAIN ANALYZE before and after on a read-only database mirror. If the fix held up, the agent opened a draft PR, with the example trace linked for reviewers.

## Takeaways

- Rewriting the GroupHistory lookup from one ORDER BY … LIMIT query across several statuses to one probe per status with UNION ALL and taking the max in Python cut the busiest group on the mirror from 935ms to 0.4ms with a warm cache.
- Reusing the already loaded project in two hot paths removed an estimated 143M extra SELECTs a week; the two occurrence-ingestion fixes moved save\_issue\_occurrence p50 from about 35ms to 30ms and p95 from about 95ms to 61ms versus the same hours the previous day.
- Eager-loading the application and token for Sentry app requests reduced two extra selects from about 1.9% to 0.16% of query volume on the control database, roughly 29M fewer queries a day.

## FAQ

### How did the agent validate SQL query changes before opening a PR?

It ran EXPLAIN ANALYZE before and after on a read-only database mirror and compared the results. The engineer also checked request latency and database load in Sentry to judge how much a removed query mattered.

### How did the agent turn trace data into a ranked list of expensive work?

It sorted spans by how often they ran and how long they took, and grouped spans by description. Django query spans carried SQL as their description, and query-source attributes supplied the file and line that ran a query.

### What did the GroupHistory query change do?

The original query asked for the newest row across several statuses with ORDER BY date\_added DESC LIMIT 1, so Postgres read and sorted every matching row. The replacement ran one probe per status, took the max in Python, and combined the results with UNION ALL; on the busiest group it went from 935ms to 0.4ms with a warm cache.

**Tags:** [Coding Agents](https://yomu.fyi/topic/coding-agents), [MCP](https://yomu.fyi/topic/mcp), [Performance](https://yomu.fyi/topic/performance), [Postgres](https://yomu.fyi/topic/postgres), [Sentry](https://yomu.fyi/topic/sentry)

- Source: [Sentry](https://blog.sentry.io/performance-wins-traces-coding-agent)
- Source URL: https://blog.sentry.io/performance-wins-traces-coding-agent
- Ingested by Yomu: 2026-11-03T18:01:11.004Z

[Read original post](https://blog.sentry.io/performance-wins-traces-coding-agent)
