Skip to main content
skill CX Codex

sql-query-optimization — Claude Code skill: analyze and speed up SQL

This skill digs into slow SQL queries on PostgreSQL and MySQL: execution plans, indexes, rewriting. Useful when a query drags and you can't tell why.

Install into Claude Code · Skills CLI (Vercel) All methods and platforms ↓
npx skills add aj-geddes/useful-ai-prompts --skill sql-query-optimization -a claude-code
Open on GitHub

What sql-query-optimization does

The skill gives your agent a procedure for analyzing SQL queries and finding bottlenecks. It covers reading execution plans with EXPLAIN, checking table statistics, indexing strategies, and query rewriting patterns. Joins, subqueries, and batch operations each get their own treatment.

Here's the layout: SKILL.md holds the trigger description and quick-start examples, while the detail lives in the references/ directory — four guides covering current-performance analysis, common optimization patterns, rewriting techniques, and batch operations. The agent pulls the relevant guide for the situation at hand.

PostgreSQL and MySQL are the stated targets. On another engine the pg_stats and EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON) examples won't carry over directly, so check what your database offers for plan and statistics inspection before trusting the output.

What it helps with

  • trace a slow query back to its cause in the execution plan
  • confirm whether an index is actually being used
  • rewrite a heavy join or subquery
  • capture a performance baseline before tuning
  • swap row-by-row inserts for batch operations

Example prompts for the agent

  • Analyze this PostgreSQL query and show me where it's slow
  • Check the execution plan for the yearly orders query and suggest indexes
  • Rewrite this join, it takes far too long

How to install sql-query-optimization

1Skills CLI (Vercel)

npx skills add aj-geddes/useful-ai-prompts --skill sql-query-optimization -a claude-code

2Manually: put the files in place

~/.claude/skills/sql-query-optimization/SKILL.md

Copy the whole skill folder; for one project use .claude/skills/sql-query-optimization/

Step by step, with commands for each platform: How to install skills in Claude Code: five ways

Questions about sql-query-optimization

How do I install sql-query-optimization in Claude?

Run npx skills add aj-geddes/useful-ai-prompts --skill sql-query-optimization -a claude-code. The skill file then lands at ~/.claude/skills/sql-query-optimization/SKILL.md.

What does sql-query-optimization do?

It walks through query performance work: reading execution plans, verifying index usage, and proposing rewrites and batch operations.

Which databases does the skill support?

PostgreSQL and MySQL are the stated targets. The reference material leans on those engines' tooling, EXPLAIN and pg_stats among them.

Source text

SKILL.md · MIT
---
name: sql-query-optimization
description: >
  Analyze and optimize SQL queries for performance. Use when improving slow
  queries, reducing execution time, or analyzing query performance in PostgreSQL
  and MySQL.
---

# SQL Query Optimization

## Table of Contents

- [Overview](#overview)
- [When to Use](#when-to-use)
- [Quick Start](#quick-start)
- [Reference Guides](#reference-guides)
- [Best Practices](#best-practices)

## Overview

Analyze SQL queries to identify performance bottlenecks and implement optimization techniques. Includes query analysis, indexing strategies, and rewriting patterns for improved performance.

## When to Use

- Slow query analysis and tuning
- Query rewriting and refactoring
- Index utilization verification
- Join optimization
- Subquery optimization
- Query plan analysis (EXPLAIN)
- Performance baseline establishment

## Quick Start

**PostgreSQL:**

```sql
-- Analyze query plan with execution time
EXPLAIN (ANALYZE, BUFFERS, FORMAT JSON)
SELECT u.id, u.email, COUNT(o.id) as order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.created_at > NOW() - INTERVAL '1 year'
GROUP BY u.id, u.email;

-- Check table statistics
SELECT * FROM pg_stats
WHERE tablename = 'users' AND attname = 'created_at';
```

## Reference Guides

Detailed implementations in the `references/` directory:

| Guide | Contents |
|---|---|
| [Analyze Current Performance](references/analyze-current-performance.md) | Analyze Current Performance |
| [Common Optimization Patterns](references/common-optimization-patterns.md) | Common Optimization Patterns |
| [Query Rewriting Techniques](references/query-rewriting-techniques.md) | Query Rewriting Techniques |
| [Batch Operations](references/batch-operations.md) | Batch Operations |

## Best Practices

### ✅ DO

- Follow established patterns and conventions
- Write clean, maintainable code
- Add appropriate documentation
- Test thoroughly before deploying

### ❌ DON'T

- Skip testing or validation
- Ignore error handling
- Hard-code configuration values

© aj-geddes · MIT · aj-geddes/useful-ai-prompts

Similar skills

All skills for Data and databases →

From the same set118

Project sponsors

Companies that keep this analytics open
Gold sponsor

Impressions, clicks and code copies — open statistics. We keep the numbers public for the same reason NeBlask itself is public. Become a sponsor