Convex cost optimizer

From Melvynx/benchmarks · View interactive version

Convex cost optimizer is an AI agent skill from the GitHub repository Melvynx/benchmarks. Use when optimizing Convex database read bandwidth, documents-read cost, query indexes, full-table scans, function-level usage dashboard findings, or code that uses .filter(), unbounded .collect(), N+1 reads, or expensive reactive Convex queries.

How to use this skill

Skill file

Convex Cost Optimizer

Reduce Convex database bandwidth and read amplification while preserving query

correctness. This skill is narrower than convex-performance-audit: use it

when the signal is specifically high database read bandwidth, high documents

read, full-table scans, index design, or expensive query syntax.

Required Context

changing schema or Convex functions.

supported, or audit the code directly and state that the ranking is

code-derived rather than usage-derived.

Dashboard Triage

Start from the Convex usage dashboard database bandwidth section:

  1. Sort by database read bandwidth or documents read.
  2. Open the function-level breakdown.
  3. Prioritize functions where "Documents Read" is much higher than "Documents

Returned".

  1. Map each hot function name to the matching file under convex/.
  2. Inspect client callsites using useQuery, usePaginatedQuery,

fetchAuthQuery, or route loaders so you understand call frequency.

A high documents-read/documents-returned ratio usually means a full-table scan,

unbounded read, or read-after-filter pattern.

Code Audit Patterns

Search for these first:

rg -n "\\.filter\\(|\\.collect\\(\\)|collect\\(\\)\\.length|for \\(.*await ctx\\.db\\.get|ctx\\.db\\.query" convex

Flag these patterns:

Fix Order

Apply the smallest high-impact fix first.

  1. Replace full scans with indexes.
  2. Bound result sets with .take(n) or .paginate(...).
  3. Replace collect().length with maintained counters or aggregate documents.
  4. Batch foreign-key lookups and avoid N+1 query loops.
  5. Return smaller DTOs from hot list functions.
  6. Denormalize or introduce digest/summary tables only when measured cost,

row size, or call frequency justifies the migration complexity.

When an index or denormalized field depends on old data being backfilled, use

the migration helper workflow instead of assuming old documents have the new

shape.

Index Syntax

Schema:

messages: defineTable({
  channelId: v.id("channels"),
  body: v.string(),
}).index("by_channelId", ["channelId"]),

Query:

const messages = await ctx.db
  .query("messages")
  .withIndex("by_channelId", (q) => q.eq("channelId", channelId))
  .take(50);

Use Convex index syntax exactly:

.index("by_organizationId_and_status", ["organizationId", "status"]).

not q.eq(q.field("fieldName"), value).

only when the query needs them.

by_org_and_status unless the single-field index is needed for _creationTime

ordering.

Correctness Checks

For every rewritten query, verify:

Verification

Run the smallest checks that cover the change:

generated API changes.

cost reduction; local checks prove syntax and behavior, not production

bandwidth savings.

Always update CHANGELOG.md for repo changes.

Use this skill with your AI agent: Open on Skill Issue to copy, download, or add to your library. Works with Claude, ChatGPT, Gemini, Cursor, Windsurf and more.