Skip to content
aj-geddes avatar

Database Query Optimization

Practical query tuning guide that uses EXPLAIN ANALYZE, indexing, and optimization checklists to cut response times.

CCM score
39 of 100
skills.sh installs
594
GitHub stars
396
last updated
Aug 21, 2026
View on GitHub

Own this skill? Claim it

What it does

  • Covers query analysis with EXPLAIN ANALYZE, indexing strategy, query rewrite techniques, and a pre-deployment optimization checklist
  • Reads plan output for sequential versus index scans, planned versus actual row counts, nested loops, and in-memory or disk sorts
  • Provides four reference guides in a references/ directory alongside a quick-start example joining users to orders
  • Designed for diagnosing slow response times, high database CPU, performance regressions, and new feature rollouts

Install

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

Installs into .agents/skills for every agent it finds here, and .claude/skills for Claude Code.

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

Details

Repository
github.com/aj-geddes/useful-ai-prompts
Categories
Databases, Debugging
First seen
Jun 3, 2026

More from aj-geddes/useful-ai-prompts

All 258

Similar to Database Query Optimization

More Databases