
Sql Optimization
Tune slow SQL, indexes, pagination, and batch patterns across MySQL, PostgreSQL, SQL Server, and Oracle before or after release.
Overview
sql-optimization is an agent skill most often used in Ship (also Build, Operate) that guides universal SQL query tuning, indexing, and performance analysis across major relational databases.
Install
npx skills add https://github.com/github/awesome-copilot --skill sql-optimizationWhat is this skill?
- Universal patterns for MySQL, PostgreSQL, SQL Server, Oracle, and other SQL engines
- Execution plan analysis with before/after inefficient vs optimized query examples
- Index strategy optimization including composite and covering index guidance
- Pagination and batch operation tuning to cut round trips and full scans
- Performance monitoring guidance aligned with cross-database best practices
Adoption & trust: 12.1k installs on skills.sh; 34.6k GitHub stars; 3/3 security scanners passed (skills.sh audits).
What problem does it solve?
Your app’s SQL is slow or unindexed and you need dialect-agnostic fixes without guessing which patterns hurt the planner.
Who is it for?
Solo builders hardening ORM-generated or hand-written SQL before launch or during a perf fire drill.
Skip if: NoSQL-only stacks, greenfield schema design from zero, or compliance-heavy migration projects without SQL context.
When should I use this skill?
User needs SQL performance optimization, query tuning, indexing strategies, execution plan analysis, or pagination/batch improvements across SQL databases.
What do I get? / Deliverables
You get rewritten queries, index recommendations, and monitoring habits that reduce scans and stabilize latency on MySQL, PostgreSQL, SQL Server, and Oracle-style workloads.
- Optimized query rewrites with rationale
- Recommended index definitions per hot access paths
- Pagination and batch patterns sized for your dialect
Recommended Skills
Journey fit
Spans multiple journey phases - primary shelf plus alternate fits below.
Performance optimization is the canonical ship/perf shelf because the skill centers on execution plans, indexing strategy, and query patterns that gate safe releases. Subphase perf matches execution plan analysis, pagination optimization, and performance monitoring guidance rather than schema design alone.
Where it fits
Rewrite N+1 order lookups into JOINs with suggested indexes before merging the billing API.
Run EXPLAIN-guided fixes on checkout queries that fail load-test thresholds.
Tune monthly reporting SQL that started timing out after user growth.
How it compares
Cross-engine SQL tuning playbook—not an ORM-specific generator or a hosted APM product.
Common Questions / FAQ
Who is sql-optimization for?
Developers shipping SaaS or APIs who maintain relational queries and want agent-guided tuning across common SQL databases.
When should I use sql-optimization?
During ship perf reviews, when refactoring hot queries in build/backend work, or in operate when dashboards or reports degrade—especially on selected SQL or whole-repo analysis.
Is sql-optimization safe to install?
It advises query and index changes; review the Security Audits panel on this page and never apply destructive DDL in production without backups and staging validation.
SKILL.md
READMESKILL.md - Sql Optimization
# SQL Performance Optimization Assistant Expert SQL performance optimization for ${selection} (or entire project if no selection). Focus on universal SQL optimization techniques that work across MySQL, PostgreSQL, SQL Server, Oracle, and other SQL databases. ## 🎯 Core Optimization Areas ### Query Performance Analysis ```sql -- ❌ BAD: Inefficient query patterns SELECT * FROM orders o WHERE YEAR(o.created_at) = 2024 AND o.customer_id IN ( SELECT c.id FROM customers c WHERE c.status = 'active' ); -- ✅ GOOD: Optimized query with proper indexing hints SELECT o.id, o.customer_id, o.total_amount, o.created_at FROM orders o INNER JOIN customers c ON o.customer_id = c.id WHERE o.created_at >= '2024-01-01' AND o.created_at < '2025-01-01' AND c.status = 'active'; -- Required indexes: -- CREATE INDEX idx_orders_created_at ON orders(created_at); -- CREATE INDEX idx_customers_status ON customers(status); -- CREATE INDEX idx_orders_customer_id ON orders(customer_id); ``` ### Index Strategy Optimization ```sql -- ❌ BAD: Poor indexing strategy CREATE INDEX idx_user_data ON users(email, first_name, last_name, created_at); -- ✅ GOOD: Optimized composite indexing -- For queries filtering by email first, then sorting by created_at CREATE INDEX idx_users_email_created ON users(email, created_at); -- For full-text name searches CREATE INDEX idx_users_name ON users(last_name, first_name); -- For user status queries CREATE INDEX idx_users_status_created ON users(status, created_at) WHERE status IS NOT NULL; ``` ### Subquery Optimization ```sql -- ❌ BAD: Correlated subquery SELECT p.product_name, p.price FROM products p WHERE p.price > ( SELECT AVG(price) FROM products p2 WHERE p2.category_id = p.category_id ); -- ✅ GOOD: Window function approach SELECT product_name, price FROM ( SELECT product_name, price, AVG(price) OVER (PARTITION BY category_id) as avg_category_price FROM products ) ranked WHERE price > avg_category_price; ``` ## 📊 Performance Tuning Techniques ### JOIN Optimization ```sql -- ❌ BAD: Inefficient JOIN order and conditions SELECT o.*, c.name, p.product_name FROM orders o LEFT JOIN customers c ON o.customer_id = c.id LEFT JOIN order_items oi ON o.id = oi.order_id LEFT JOIN products p ON oi.product_id = p.id WHERE o.created_at > '2024-01-01' AND c.status = 'active'; -- ✅ GOOD: Optimized JOIN with filtering SELECT o.id, o.total_amount, c.name, p.product_name FROM orders o INNER JOIN customers c ON o.customer_id = c.id AND c.status = 'active' INNER JOIN order_items oi ON o.id = oi.order_id INNER JOIN products p ON oi.product_id = p.id WHERE o.created_at > '2024-01-01'; ``` ### Pagination Optimization ```sql -- ❌ BAD: OFFSET-based pagination (slow for large offsets) SELECT * FROM products ORDER BY created_at DESC LIMIT 20 OFFSET 10000; -- ✅ GOOD: Cursor-based pagination SELECT * FROM products WHERE created_at < '2024-06-15 10:30:00' ORDER BY created_at DESC LIMIT 20; -- Or using ID-based cursor SELECT * FROM products WHERE id > 1000 ORDER BY id LIMIT 20; ``` ### Aggregation Optimization ```sql -- ❌ BAD: Multiple separate aggregation queries SELECT COUNT(*) FROM orders WHERE status = 'pending'; SELECT COUNT(*) FROM orders WHERE status = 'shipped'; SELECT COUNT(*) FROM orders WHERE status = 'delivered'; -- ✅ GOOD: Single query with conditional aggregation SELECT COUNT(CASE WHEN status = 'pending' THEN 1 END) as pending_count, COUNT(CASE WHEN status = 'shipped' THEN 1 END) as shipped_count, COUNT(CASE WHEN status = 'delivered' THEN 1 END) as delivered_count FROM orders; ``` ## 🔍 Query Anti-Patter