Query optimize

From berkcangumusisik/claude-code-practices · View interactive version

Query optimize is an AI agent skill from the GitHub repository berkcangumusisik/claude-code-practices. Optimizes database queries - adds indexes, rewrites N+1s, batches requests

How to use this skill

Skill file

Query Optimization

Target: $1 (or scan src/ for all DB interactions)

  1. Find all database queries:
grep -rn "\.find\|\.findMany\|\.query\|\.select\|db\.\|prisma\.\|knex\." ${1:-src/} --include="*.ts" | head -40
  1. Check for N+1 patterns:
// ❌ N+1: querying inside a loop
for (const user of users) {
  user.posts = await db.posts.findMany({ where: { userId: user.id } })
}

// ✅ Fix: batch with include or Promise.all
const users = await db.users.findMany({ include: { posts: true } })
  1. Check for missing select (over-fetching):
// ❌ Fetches all 40 columns
await db.users.findMany()

// ✅ Only what's needed
await db.users.findMany({ select: { id: true, name: true, email: true } })
  1. Propose missing indexes for frequently-filtered columns.
  1. Show before/after for each optimization. Apply on confirmation.
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.