aj-geddes avatar

Database Query Optimization

Speeds up slow database queries by walking you through execution plan analysis, indexing strategies, and query rewrites. You'll use EXPLAIN ANALYZE to spot sequential scans that should be index scans, then apply fixes based on the specific bottleneck. Works well when you're debugging performance regressions or trying to reduce database CPU before scaling up hardware. The reference guides cover common patterns like avoiding N+1 queries and choosing between different index types. Honest take: this is solid for the 80% of query problems that come down to missing indexes or bad joins, but complex optimization still needs domain expertise.

skills.sh installs
593
GitHub stars
379
last updated
Aug 21, 2026

Install

$ npx -y skills add aj-geddes/useful-ai-prompts --skill database-query-optimization --agent claude-code

Installs into .claude/skills of the current project.

Files

Database Query Optimization

Table of Contents

Overview

Slow database queries are a common performance bottleneck. Optimization through indexing, efficient queries, and caching dramatically improves application performance.

When to Use

  • Slow response times
  • High database CPU usage
  • Performance regression
  • New feature deployment
  • Regular maintenance

Quick Start

Minimal working example:

-- Analyze query performance

EXPLAIN ANALYZE
SELECT users.id, users.name, COUNT(orders.id) as order_count
FROM users
LEFT JOIN orders ON users.id = orders.user_id
WHERE users.created_at > '2024-01-01'
GROUP BY users.id, users.name
ORDER BY order_count DESC;

-- Results show:
-- - Seq Scan (slow) vs Index Scan (fast)
-- - Rows: actual vs planned (high variance = bad)
-- - Execution time (milliseconds)

-- Key metrics:
-- - Sequential Scan: Full table read (slow)
-- - Index Scan: Uses index (fast)
-- - Nested Loop: Joins with loops
-- - Sort: In-memory or disk sort

Reference Guides

Sponsors

Details

Repository
github.com/aj-geddes/useful-ai-prompts
Categories
Backend & APIs, AI & Agent Building, Debugging, Productivity & Planning, Databases
First seen
Jun 3, 2026

More from aj-geddes/useful-ai-prompts

All 258
1recoupable avatarrecoup-apirecoupable/skillsCall the Recoupable API from the sandbox to fetch artist data, socials, organizations, research, documents and any other platform resource — and to invoke exter550skills.sh installs
2aj-geddes avatarpayment-gateway-integrationaj-geddes/useful-ai-promptsIntegrate payment gateways like Stripe, PayPal, and Square with backends for payment processing, subscription management, and webhook handling. Use when buildin529skills.sh installs
3sickn33 avatarmoodle-external-api-developmentsickn33/antigravity-awesome-skillsmoodle external api development526skills.sh installs
4bobmatnyc avatargolang-grpcbobmatnyc/claude-mpm-skillsProduction gRPC in Go: protobuf layout, codegen, interceptors, deadlines, error codes, streaming, health checks, TLS, and testing with bufconn518skills.sh installs
5yoanbernabeu avatarsupabase-reportyoanbernabeu/supabase-pentest-skillsGenerate a comprehensive Markdown security audit report with executive summary, findings, and remediation guidance.516skills.sh installs
6firebase avatarfirebase-firestorefirebase/skillsfirebase firestore506skills.sh installs