Command Palette

Search for a command to run...

UnylyUnyly
Назад к скиллам

sql-optimizer

БесплатноБез исполняемых скриптовНе проверен

Analyze and optimize SQL queries for performance. Use when a user asks to optimize a query, speed up a slow query, analyze a query plan, add indexes, fix N+1…

TerminalSkillsот TerminalSkills★ 221 звезда на GitHub

Об этом скилле

SQL Optimizer

Overview

Analyze SQL queries for performance problems and produce optimized versions with appropriate indexes. Covers query rewriting, index recommendations, execution plan analysis, and common anti-patterns.

Instructions

When a user asks you to optimize a SQL query or fix slow database performance, follow these steps:

Step 1: Get the query and context

Determine:

  • The full SQL query to optimize
  • Database engine (PostgreSQL, MySQL, SQLite)
  • Table sizes (approximate row counts)
  • Existing indexes
  • Current execution time

If you have access to the database, gather this yourself:

-- PostgreSQL: Check table sizes
SELECT relname AS table_name, reltuples::bigint AS row_count
FROM pg_class
WHERE relkind = 'r' AND relnamespace = (
  SELECT oid FROM pg_namespace WHERE nspname = 'public'
)
ORDER BY reltuples DESC;

-- PostgreSQL: Check existing indexes
SELECT indexname, indexdef
FROM pg_indexes
WHERE tablename = 'orders';

Step 2: Analyze the execution plan

-- PostgreSQL (runs the statement, see the warning below)
EXPLAIN (ANALYZE, BUFFERS)
SELECT id, total FROM orders WHERE status = 'pending';

-- MySQL 8.0.18+ (runs the statement)
EXPLAIN ANALYZE
SELECT id, total FROM orders WHERE status = 'pending';

-- MySQL, plan only (does not run the query)
EXPLAIN FORMAT=JSON
SELECT id, total FROM orders WHERE status = 'pending';

-- SQLite, plan only
EXPLAIN QUERY PLAN
SELECT id, total FROM orders WHERE status = 'pending';

Warning: EXPLAIN ANALYZE executes the statement, so an INSERT, UPDATE or DELETE really changes data. Wrap it in a transaction and roll back:

BEGIN;
EXPLAIN ANALYZE DELETE FROM orders WHERE status = 'cancelled' AND created_at < '2024-01-01';
ROLLBACK;

SQLite prints one row per plan step, for example SEARCH orders USING COVERING INDEX idx_orders_customer_status (customer_id=? AND status=?). A covering index means the table row is never read.

Look for these red flags in the plan:

  • Seq Scan on large tables (missing index)
  • Nested Loop with large row counts (N+1 pattern)
  • Sort with high cost (missing index for ORDER BY)
  • Hash Join when a smaller join would suffice
  • Rows removed by filter much larger than rows returned (bad selectivity)

Step 3: Apply optimizations

Anti-pattern: SELECT * when you need specific columns

-- Before (fetches all columns, prevents covering index use)
SELECT * FROM orders WHERE status = 'pending';

-- After (fetch only needed columns)
SELECT id, customer_id, total, created_at
FROM orders WHERE status = 'pending';

Anti-pattern: Missing index on WHERE/JOIN columns

-- If this query is slow:
SELECT * FROM orders WHERE customer_id = 123 AND status = 'pending';

-- Add a composite index:
CREATE INDEX idx_orders_customer_status ON orders (customer_id, status);

Anti-pattern: N+1 queries in application code

-- Before: 1 query + N queries
SELECT id FROM orders WHERE date > '2024-01-01';
-- Then for each order:
SELECT * FROM order_items WHERE order_id = ?;

-- After: Single query with JOIN
SELECT o.id, o.total, oi.product_name, oi.quantity, oi.price
FROM orders o
JOIN order_items oi ON o.id = oi.order_id
WHERE o.date > '2024-01-01';

Anti-pattern: Functions on indexed columns

-- Before (MySQL: YEAR() blocks index use on created_at; PostgreSQL and SQLite have no YEAR())
SELECT * FROM users WHERE YEAR(created_at) = 2024;

-- After (uses index on created_at)
SELECT * FROM users
WHERE created_at >= '2024-01-01' AND created_at < '2025-01-01';

Anti-pattern: Correlated subquery that can be a JOIN

-- Before (executes subquery for every row)
SELECT name, (
  SELECT COUNT(*) FROM orders WHERE orders.customer_id = customers.id
) AS order_count
FROM customers;

-- After (single pass with JOIN)
SELECT c.name, COUNT(o.id) AS order_count
FROM customers c
LEFT JOIN orders o ON c.id = o.customer_id
GROUP BY c.id, c.name;

Anti-pattern: OFFSET for deep pagination

-- Before (scans and discards 10000 rows)
SELECT * FROM products ORDER BY id LIMIT 20 OFFSET 10000;

-- After (keyset pagination, uses index)
SELECT * FROM products WHERE id > 10000 ORDER BY id LIMIT 20;

Step 4: Recommend indexes

Follow these rules for index design:

  1. Index columns used in WHERE, JOIN ON, and ORDER BY clauses
  2. Put equality conditions first, then range conditions in composite indexes
  3. Consider covering indexes that include SELECT columns to avoid table lookups
  4. Do not create indexes on low-cardinality columns (e.g., boolean flags) alone
  5. Remove unused or duplicate indexes
-- Composite index for: WHERE status = 'active' AND created_at > '2024-01-01' ORDER BY name
CREATE INDEX idx_users_status_created_name
ON users (status, created_at, name);

Step 5: Report the results

Present:

  1. The original query with identified problems
  2. The optimized query
  3. Index recommendations with CREATE INDEX sta
Полный SKILL.md на GitHub

Установить sql-optimizer в Claude Code и Claude Desktop

Зарегайся, чтобы установить скилл

Создай бесплатный аккаунт, чтобы открыть команду установки и сохранить скилл в библиотеку.

  • Открой команду установки в одну строку
  • Сохраняй скиллы в синхронизируемую библиотеку
  • Уведомления, когда скиллы обновляются
Зарегаться бесплатноУ меня уже есть аккаунт

Разрешённые инструменты

Инструменты, которые скиллу разрешено вызывать.

Без ограничений — скилл может использовать любой инструмент.

Вложенные файлы

_scores.json

FAQ

Что делает скилл sql-optimizer?

Analyze and optimize SQL queries for performance. Use when a user asks to optimize a query, speed up a slow query, analyze a query plan, add indexes, fix N+1 queries, reduce query time, tune database performance, or rewrite SQL for efficiency. Supports PostgreSQL, MySQL, and SQLite.

Как установить скилл sql-optimizer?

Скопируй папку скилла в ~/.claude/skills (вкладка Claude Code выше делает это одной командой), либо поставь как плагин.

Скилл sql-optimizer запускает скрипты?

Нет, скилл состоит только из инструкций (SKILL.md), без исполняемых скриптов.

Похожие скиллы

Сравнить sql-optimizer с