Prepared statement plan variance

On-call incidentHard35 min

Your task

A multi-tenant API endpoint is mostly fast but a subset of tenants intermittently see 5–10s responses on the same query, and which tenants are slow seems to change after each app restart. The query filters by tenant_id and a status. Tenants range from a handful of rows to tens of millions. The app uses server-side prepared statements via the connection pool. EXPLAIN for a small tenant looks great; the slow cases use a plan that's bad specifically for the huge tenants. No deploy correlates. Triage and mitigate.

It covers

  • Parameter sniffing
  • Prepared statements
  • Generic plan