Query Optimizer Skill
Analyze and optimize SQL queries for better performance and efficiency.
Instructions
You are a database performance optimization expert. When invoked:
- Analyze Query Performance:
- Use EXPLAIN/EXPLAIN ANALYZE to understand execution plan
- Identify slow queries from logs
- Measure query execution time
- Detect full table scans and missing indexes
- Identify Bottlenecks:
- Find N+1 query problems
- Detect inefficient JOINs
- Identify missing or unused indexes
- Spot suboptimal WHERE clauses
- Optimize Queries:
- Add appropriate indexes
- Rewrite queries for better performance
- Suggest caching strategies
- Recommend query restructuring
- Provide Recommendations:
- Index creation suggestions
- Query rewriting alternatives
- Database configuration tuning
- Monitoring and alerting setup
Supported Databases
- SQL: PostgreSQL, MySQL, MariaDB, SQL Server, SQLite
- Analysis Tools: EXPLAIN, EXPLAIN ANALYZE, Query Profiler
- Monitoring: pg_stat_statements, slow query log, performance schema
Usage Examples
@query-optimizer
@query-optimizer --analyze-slow-queries
@query-optimizer --suggest-indexes
@query-optimizer --explain SELECT * FROM users WHERE email = 'test@example.com'
@query-optimizer --fix-n-plus-one
Query Analysis Tools
PostgreSQL - EXPLAIN ANALYZE
sql
-- Basic EXPLAIN
EXPLAIN
SELECT u.id, u.username, COUNT(o.id) as order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.active = true
GROUP BY u.id, u.username;
-- EXPLAIN ANALYZE - actually runs the query
EXPLAIN ANALYZE
SELECT u.id, u.username, COUNT(o.id) as order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.active = true
GROUP BY u.id, u.username;
-- EXPLAIN with all options (PostgreSQL)
EXPLAIN (ANALYZE, BUFFERS, VERBOSE, FORMAT JSON)
SELECT * FROM orders
WHERE user_id = 123
AND created_at >= '2024-01-01';
Reading EXPLAIN Output:
Seq Scan on users (cost=0.00..1234.56 rows=10000 width=32)
Filter: (active = true)
-- Seq Scan = Sequential Scan (full table scan) - BAD for large tables
-- cost=0.00..1234.56 = startup cost..total cost
-- rows=10000 = estimated rows
-- width=32 = average row size in bytes
Index Scan using idx_users_email on users (cost=0.29..8.30 rows=1 width=32)
Index Cond: (email = 'test@example.com'::text)
-- Index Scan = Using index - GOOD
-- Much lower cost than Seq Scan
-- rows=1 = accurate estimate
MySQL - EXPLAIN
sql
-- MySQL EXPLAIN
EXPLAIN
SELECT u.id, u.username, COUNT(o.id) as order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
WHERE u.active = true
GROUP BY u.id, u.username;
-- EXPLAIN with execution stats (MySQL 8.0+)
EXPLAIN ANALYZE
SELECT * FROM orders
WHERE user_id = 123;
-- Show warnings for optimization info
EXPLAIN
SELECT * FROM users WHERE email = 'test@example.com';
SHOW WARNINGS;
MySQL EXPLAIN Output:
+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
| 1 | SIMPLE | users | ALL | NULL | NULL | NULL | NULL | 1000 | Using where |
+----+-------------+-------+------+---------------+---------+---------+-------+------+-------------+
-- type=ALL means full table scan - BAD
-- key=NULL means no index used
+----+-------------+-------+------+---------------+----------------+---------+-------+------+-------+
| id | select_type | table | type | possible_keys | key | key_len | ref | rows | Extra |
+----+-------------+-------+------+---------------+----------------+---------+-------+------+-------+
| 1 | SIMPLE | users | ref | idx_users_email| idx_users_email| 767 | const | 1 | NULL |
+----+-------------+-------+------+---------------+----------------+---------+-------+------+-------+
-- type=ref means index lookup - GOOD
-- key shows index being used
Common Performance Issues
1. Missing Indexes
Problem:
sql
-- Slow query - full table scan
SELECT * FROM users WHERE email = 'john@example.com';
-- EXPLAIN shows:
-- Seq Scan on users (cost=0.00..1500.00 rows=1 width=100)
-- Filter: (email = 'john@example.com')
Solution:
sql
-- Add index on email column
CREATE INDEX idx_users_email ON users(email);
-- Now EXPLAIN shows:
-- Index Scan using idx_users_email on users (cost=0.29..8.30 rows=1 width=100)
-- Index Cond: (email = 'john@example.com')
-- Query becomes 100x faster
2. N+1 Query Problem
Problem:
javascript
// ORM code causing N+1 queries
const users = await User.findAll(); // 1 query
for (const user of users) {
const orders = await Order.findAll({
where: { userId: user.id } // N queries (one per user)
});
console.log(`${user.name}: ${orders.length} orders`);
}
// Total: 1 + N queries for N users
// For 100 users = 101 queries!
Solution:
javascript
// Use eager loading - single query with JOIN
const users = await User.findAll({
include: [{
model: Order,
attributes: ['id', 'total_amount']
}]
});
for (const user of users) {
console.log(`${user.name}: ${user.orders.length} orders`);
}
// Total: 1 query regardless of user count
SQL Equivalent:
sql
-- Instead of multiple queries:
SELECT * FROM users;
SELECT * FROM orders WHERE user_id = 1;
SELECT * FROM orders WHERE user_id = 2;
-- ... (N more queries)
-- Use single JOIN query:
SELECT
u.id,
u.name,
COUNT(o.id) as order_count
FROM users u
LEFT JOIN orders o ON u.id = o.user_id
GROUP BY u.id, u.name;
3. SELECT * Inefficiency
Problem:
sql
-- Fetching all columns when only need few
SELECT * FROM products
WHERE category_id = 5;
-- Fetches: id, name, description (large text), image_url, specs (json),
-- price, stock, created_at, updated_at, etc.
Solution:
sql
-- Only select needed columns
SELECT id, name, price, stock
FROM products
WHERE category_id = 5;
-- Benefits:
-- - Less data transferred
-- - Faster query execution
-- - Lower memory usage
-- - Can use covering indexes
4. Inefficient Pagination
Problem:
sql
-- OFFSET becomes slow with large offsets
SELECT * FROM users
ORDER BY created_at DESC
LIMIT 20 OFFSET 10000;
-- Database must:
-- 1. Sort all rows
-- 2. Skip 10,000 rows
-- 3. Return next 20
-- Gets slower as offset increases
Solution:
sql
-- Use cursor-based (keyset) pagination
SELECT * FROM users
WHERE created_at < '2024-01-01 12:00:00'
AND (created_at < '2024-01-01 12:00:00' OR id < 12345)
ORDER BY created_at DESC, id DESC
LIMIT 20;
-- Or with indexed column:
SELECT * FROM users
WHERE id < 10000
ORDER BY id DESC
LIMIT 20;
-- Benefits:
-- - Consistent performance regardless of page
-- - Uses index efficiently
-- - No need to skip rows
5. Function on Indexed Column
Problem:
sql
-- Function prevents index usage
SELECT * FROM users
WHERE LOWER(email) = 'john@example.com';
-- EXPLAIN shows Seq Scan (index not used)
Solution 1 - Store lowercase:
sql
-- Add computed column
ALTER TABLE users ADD COLUMN email_lower VARCHAR(255)
GENERATED ALWAYS AS (LOWER(email)) STORED;
CREATE INDEX idx_users_email_lower ON users(email_lower);
-- Query:
SELECT * FROM users
WHERE email_lower = 'john@example.com';
Solution 2 - Functional index (PostgreSQL):
sql
-- Create index on function result
CREATE INDEX idx_users_email_lower ON users(LOWER(email));
-- Now original query uses index
SELECT * FROM users
WHERE LOWER(email) = 'john@example.com';
Solution 3 - Case-insensitive collation:
sql
-- PostgreSQL - use citext type
ALTER TABLE users ALTER COLUMN email TYPE citext;
-- Query without LOWER:
SELECT * FROM users WHERE email = 'john@example.com';
-- Automatically case-insensitive
6. Inefficient JOINs
Problem:
sql
-- Multiple JOINs without proper indexes
SELECT
u.username,
o.id as order_id,
p.name as product_name
FROM users u
JOIN orders o ON u.id = o.user_id
JOIN order_items oi ON o.id = oi.order_id
JOIN products p ON oi.product_id = p.id
WHERE u.email = 'john@example.com';
-- Slow if missing indexes on:
-- - users.email
-- - orders.user_id
-- - order_items.order_id
-- - order_items.product_id
Solution:
sql
-- Add necessary indexes
CREATE INDEX idx_users_email ON users(email);
CREATE INDEX idx_orders_user_id ON orders(user_id);
CREATE INDEX idx_order_items_order_id ON order_items(order_id);
CREATE INDEX idx_order_items_product_id ON order_items(product_id);
-- Now query uses indexes for all JOINs
-- EXPLAIN will show "Index Scan" for each table
7. OR Conditions
Problem:
sql
-- OR prevents efficient index usage
SELECT * FROM users
WHERE username = 'john' OR email = 'john@example.com';
-- May not use indexes optimally
Solution:
sql
-- Use UNION for better index usage
SELECT * FROM users WHERE username = 'john'
UNION
SELECT * FROM users WHERE email = 'john@example.com';
-- Each subquery uses its own index
-- Deduplicates results automatically
8. NOT IN with Subquery
Problem:
sql
-- Slow subquery execution
SELECT * FROM users
WHERE id NOT IN (
SELECT user_id FROM banned_users
);
-- Can be very slow with large subquery results
Solution:
sql
-- Use LEFT JOIN with NULL check
SELECT u.*
FROM users u
LEFT JOIN banned_users bu ON u.id = bu.user_id
WHERE bu.user_id IS NULL;
-- Or use NOT EXISTS (often faster):
SELECT u.*
FROM users u
WHERE NOT EXISTS (
SELECT 1 FROM banned_users bu
WHERE bu.user_id = u.id
);
Index Optimization
When to Add Indexes
Add indexes for:
- Primary keys (automatic in most databases)
- Foreign keys (critical for JOINs)
- Columns in WHERE clauses
- Columns in ORDER BY clauses
- Columns in GROUP BY clauses
- Columns in JOIN conditions
- Columns with high cardinality (many unique values)
Index Types
B-Tree Index (Default):
sql
-- Best for: equality (=) and range (<, >, BETWEEN) queries
CREATE INDEX idx_users_created_at ON users(created_at);
-- Good for:
SELECT * FROM users WHERE created_at > '2024-01-01';
SELECT * FROM users WHERE created_at BETWEEN '2024-01-01' AND '2024-12-31';
Composite Index:
sql
-- Index on multiple columns
CREATE INDEX idx_orders_user_status ON orders(user_id, status);
-- Used for queries filtering both columns
SELECT * FROM orders WHERE user_id = 123 AND status = 'pending';
-- Also used for queries on first column only
SELECT * FROM orders WHERE user_id = 123;
-- NOT used for queries on second column only
SELECT * FROM orders WHERE status = 'pending'; -- Won't use this index
-- Column order matters! Most selective first
Partial Index (PostgreSQL):
sql
-- Index only subset of rows
CREATE INDEX idx_active_users ON users(email)
WHERE active = true;
-- Smaller index, faster queries for active users
SELECT * FROM users WHERE email = 'john@example.com' AND active = true;
GIN Index (PostgreSQL - for arrays, JSONB, full-text):
sql
-- For JSONB columns
CREATE INDEX idx_products_metadata ON products USING GIN(metadata);
-- Query JSONB data
SELECT * FROM products
WHERE metadata @> '{"brand": "Apple"}';
-- For array columns
CREATE INDEX idx_tags ON posts USING GIN(tags);
-- Query arrays
SELECT * FROM posts WHERE tags @> ARRAY['postgresql'];
Full-Text Search Index:
sql
-- PostgreSQL
CREATE INDEX idx_products_search ON products
USING GIN(to_tsvector('english', name || ' ' || description));
-- Full-text search query
SELECT * FROM products
WHERE to_tsvector('english', name || ' ' || description)
@@ to_tsquery('english', 'laptop & gaming');
Covering Index