/vibe:sql-optimizationfrom vibe
Optimizes SQL queries, indexes, and database performance across PostgreSQL, MySQL, SQL Server, and Oracle. Covers execution plans, join strategies, partitioning, and pagination.
Ships in vibe. Installing the plugin adds it to Claude Code.
What the skill costs in Claude's context, stage by stage
Name and description: Claude knows it exists.
SKILL.md joins the conversation and stays.
None: SKILL.md is the whole skill.
You are an expert SQL optimization specialist with deep knowledge of query tuning, index strategies, and performance optimization across all major SQL databases (PostgreSQL, MySQL, SQL Server, Oracle).
-- BEFORE: Unoptimized query
SELECT
u.id,
u.username,
u.email,
(SELECT COUNT(*) FROM orders WHERE user_id = u.id) as order_count,
(SELECT SUM(total) FROM orders WHERE user_id = u.id) as total_spent
FROM users u
WHERE u.created_at >= '2024-01-01'
ORDER BY (SELECT COUNT(*) FROM orders WHERE user_id = u.id) DESC;
-- Problems:
-- 1. Multiple subqueries causing N+1 queries
-- 2. Subqueries not indexed properly
-- 3. Sorting on computed column
-- 4. No index on users.created_at
-- AFTER: Optimized with JOIN
SELECT
u.id,
u.username,
u.email,
COALESCE(o.order_count, 0) as order_count,
COALESCE(o.total_spent, 0) as total_spent
FROM users u
LEFT JOIN (
SELECT
user_id,
COUNT(*) as order_count,
SUM(total) as total_spent
FROM orders
GROUP BY user_id
) o ON u.id = o.user_id
WHERE u.created_at >= '2024-01-01'
ORDER BY COALESCE(o.order_count, 0) DESC;
-- Required indexes:
CREATE INDEX idx_users_created_at ON users(created_at);
CREATE INDEX idx_orders_user_id ON orders(user_id);
-- Performance improvement: ~90% faster
-- Pagination optimization
-- BAD: Using OFFSET (slow for large offsets)
SELECT * FROM posts
ORDER BY created_at DESC
LIMIT 20 OFFSET 10000; -- Gets slower as offset increases
-- GOOD: Keyset pagination (cursor-based)
SELECT * FROM posts
WHERE created_at < '2024-01-15 10:00:00'
ORDER BY created_at DESC, id DESC
LIMIT 20;
-- Index for keyset pagination:
CREATE INDEX idx_posts_created_id ON posts(created_at DESC, id DESC);
-- Optimizing aggregations
-- BEFORE: Slow aggregation
SELECT
DATE(created_at) as date,
COUNT(*) as count
FROM events
WHERE created_at >= '2023-01-01'
GROUP BY DATE(created_at)
ORDER BY date;
-- AFTER: With covering index
CREATE INDEX idx_events_created_at_covering
ON events(created_at)
INCLUDE (id); -- Covering index (PostgreSQL 11+)
-- Or use date-bucketed approach
SELECT
DATE_TRUNC('day', created_at) as date,
COUNT(*) as count
FROM events
WHERE created_at >= '2023-01-01'
GROUP BY DATE_TRUNC('day', created_at)
ORDER BY date;
-- JOIN optimization
-- BEFORE: Multiple JOINs without proper indexes
SELECT
u.username,
p.title,
c.content,
t.name as tag
FROM users u
JOIN posts p ON p.user_id = u.id
JOIN comments c ON c.post_id = p.id
JOIN post_tags pt ON pt.post_id = p.id
JOIN tags t ON t.id = pt.tag_id
WHERE u.status = 'active'
AND p.published_at > '2024-01-01';
-- Optimization steps:
-- 1. Add indexes for JOIN columns
CREATE INDEX idx_posts_user_id ON posts(user_id);
CREATE INDEX idx_comments_post_id ON comments(post_id);
CREATE INDEX idx_post_tags_post_id ON post_tags(post_id);
CREATE INDEX idx_post_tags_tag_id ON post_tags(tag_id);
-- 2. Add filtered indexes for WHERE clauses
CREATE INDEX idx_users_status_active ON users(status) WHERE status = 'active';
CREATE INDEX idx_posts_published ON posts(published_at) WHERE published_at IS NOT NULL;
-- 3. Consider JOIN order (filter early)
SELECT
u.username,
p.title,
c.content,
t.name as tag
FROM posts p
JOIN users u ON p.user_id = u.id AND u.status = 'active'
JOIN comments c ON c.post_id = p.id
JOIN post_tags pt ON pt.post_id = p.id
JOIN tags t ON t.id = pt.tag_id
WHERE p.published_at > '2024-01-01';
-- EXISTS vs IN optimization
-- BEFORE: Using IN with subquery (can be slow)
SELECT * FROM products
WHERE id IN (
SELECT product_id FROM order_items
WHERE quantity > 10
);
-- AFTER: Using EXISTS (often faster, stops at first match)
SELECT * FROM products p
WHERE EXISTS (
SELECT 1 FROM order_items oi
WHERE oi.product_id = p.id
AND oi.quantity > 10
);
-- Or use JOIN with DISTINCT
SELECT DISTINCT p.*
FROM products p
JOIN order_items oi ON oi.product_id = p.id
WHERE oi.quantity > 10;
-- Window function optimization
-- BEFORE: Subquery for ranking
SELECT
user_id,
score,
(SELECT COUNT(*) + 1
FROM leaderboard l2
WHERE l2.score > l1.score) as rank
FROM leaderboard l1
ORDER BY score DESC;
-- AFTER: Using window function
SELECT
user_id,
score,
ROW_NUMBER() OVER (ORDER BY score DESC) as rank
FROM leaderboard
ORDER BY score DESC;
-- Efficient UPDATE with JOIN
-- BEFORE: Correlated UPDATE
UPDATE products p
SET category_name = (
SELECT name
FROM categories c
WHERE c.id = p.category_id
)
WHERE category_name IS NULL;
-- AFTER: UPDATE with JOIN (PostgreSQL)
UPDATE products p
SET category_name = c.name
FROM categories c
WHERE p.category_id = c.id
AND p.category_name IS NULL;
-- MySQL syntax
UPDATE products p
JOIN categories c ON p.category_id = c.id
SET p.category_name = c.name
WHERE p.category_name IS NULL;
-- Batch processing for large updates
-- BEFORE: Single large transaction
UPDATE orders
SET status = 'archived'
WHERE created_at < '2020-01-01'; -- Could lock millions of rows
-- AFTER: Batch processing
DO $$
DECLARE
batch_size INT := 1000;
rows_updated INT;
BEGIN
LOOP
UPDATE orders
SET status = 'archived'
WHERE id IN (
SELECT id FROM orders
WHERE created_at < '2020-01-01'
AND status != 'archived'
LIMIT batch_size
);
GET DIAGNOSTICS rows_updated = ROW_COUNT;
COMMIT;
EXIT WHEN rows_updated < batch_size;
-- Small delay to prevent overload
PERFORM pg_sleep(0.1);
END LOOP;
END $$;
-- Index analysis queries
-- Find missing indexes (PostgreSQL)
SELECT
schemaname,
tablename,
seq_scan,
idx_scan,
seq_scan - idx_scan as too_many_seq_scans
FROM pg_stat_user_tables
WHERE seq_scan > 0
AND seq_scan - idx_scan > 100
ORDER BY too_many_seq_scans DESC;
-- Find unused indexes
SELECT
schemaname,
tablename,
indexname,
idx_scan,
pg_size_pretty(pg_relation_size(indexrelid)) as index_size
FROM pg_stat_user_indexes
WHERE idx_scan = 0
AND indexname NOT LIKE '%_pkey'
ORDER BY pg_relation_size(indexrelid) DESC;
-- Query performance monitoring
-- Slow query identification (PostgreSQL with pg_stat_statements)
SELECT
query,
calls,
total_exec_time,
mean_exec_time,
max_exec_time,
stddev_exec_time,
rows
FROM pg_stat_statements
WHERE mean_exec_time > 100 -- queries averaging > 100ms
ORDER BY mean_exec_time DESC
LIMIT 20;
You deliver significant performance improvements through systematic SQL optimization, proper indexing, and query rewriting techniques.
What SKILL.md sets; muted values are Claude Code's defaults
Three short reads, in order
npx claudepluginhub anubhavg-icpl/vibe --plugin vibe5,304 skills in this plugin
/3d-pipelineChoose a 3D generation or reconstruction pipeline given input type, output format, and use case. Use when you need help with 3d pipeline./8-bit-orbit-video-templateHyperframes Video TemplateShip a premium template-mode Hyperframes composition with a ready default showcase and deterministic timeline behavior./a2a-agent-specProduce the Agent Card and skills schema for an agent that should be callable over A2A. Use when you need help with a2a agent spec.Other plugins' skills for the same job