optimizing-sql-queries

Execute use when you need to work with query optimization. This skill provides query performance analysis with comprehensive guidance and automation. Trigger with phrases like "optimize queries", "analyze performance", or "improve query speed".

25 stars

Best use case

optimizing-sql-queries is best used when you need a repeatable AI agent workflow instead of a one-off prompt.

Execute use when you need to work with query optimization. This skill provides query performance analysis with comprehensive guidance and automation. Trigger with phrases like "optimize queries", "analyze performance", or "improve query speed".

Teams using optimizing-sql-queries should expect a more consistent output, faster repeated execution, less prompt rewriting.

When to use this skill

  • You want a reusable workflow that can be run more than once with consistent structure.

When not to use this skill

  • You only need a quick one-off answer and do not need a reusable workflow.
  • You cannot install or maintain the underlying files, dependencies, or repository context.

Installation

Claude Code / Cursor / Codex

$curl -o ~/.claude/skills/optimizing-sql-queries/SKILL.md --create-dirs "https://raw.githubusercontent.com/ComeOnOliver/skillshub/main/skills/jeremylongshore/claude-code-plugins-plus-skills/optimizing-sql-queries/SKILL.md"

Manual Installation

  1. Download SKILL.md from GitHub
  2. Place it in .claude/skills/optimizing-sql-queries/SKILL.md inside your project
  3. Restart your AI agent — it will auto-discover the skill

How optimizing-sql-queries Compares

Feature / Agentoptimizing-sql-queriesStandard Approach
Platform SupportNot specifiedLimited / Varies
Context Awareness High Baseline
Installation ComplexityUnknownN/A

Frequently Asked Questions

What does this skill do?

Execute use when you need to work with query optimization. This skill provides query performance analysis with comprehensive guidance and automation. Trigger with phrases like "optimize queries", "analyze performance", or "improve query speed".

Where can I find the source code?

You can find the source code on GitHub using the link provided at the top of the page.

SKILL.md Source

# SQL Query Optimizer

## Overview

Rewrite SQL queries for maximum performance by eliminating anti-patterns, restructuring JOINs, leveraging window functions, and applying database-specific optimizations for PostgreSQL and MySQL. This skill takes a slow query and its execution plan as input and produces an optimized version with measurable improvement, along with any supporting index changes needed.

## Prerequisites

- The slow SQL query text and its current execution time
- `EXPLAIN ANALYZE` output (PostgreSQL) or `EXPLAIN FORMAT=JSON` output (MySQL) for the query
- Table row counts and approximate data distribution for involved tables
- `psql` or `mysql` CLI for testing rewrites
- Knowledge of the application's acceptable result ordering and NULL handling requirements

## Instructions

1. Examine the original query structure and identify common anti-patterns:
   - `SELECT *` instead of specific columns (forces unnecessary I/O)
   - `WHERE column IN (SELECT ...)` that can be rewritten as `JOIN` or `EXISTS`
   - `DISTINCT` used to mask duplicate rows from incorrect JOINs
   - Functions applied to indexed columns in WHERE clauses (`WHERE UPPER(name) = 'FOO'`)
   - `OR` conditions that prevent index usage
   - `NOT IN` with nullable columns (produces wrong results and poor plans)

2. Analyze the execution plan to identify the most expensive operation nodes. Focus optimization effort on the node consuming the most time or processing the most rows.

3. Rewrite subqueries as JOINs where possible. Convert correlated subqueries to lateral joins (PostgreSQL) or derived tables. Replace `IN (SELECT ...)` with `EXISTS (SELECT 1 ...)` for existence checks since EXISTS short-circuits after the first match.

4. Optimize JOIN ordering for the query planner: place the most selective table (fewest matching rows after WHERE filters) as the driving table. Use `JOIN` hints only as a last resort since the optimizer usually picks the correct order with accurate statistics.

5. Replace multiple OR conditions on the same column with `IN (...)`: change `WHERE status = 'active' OR status = 'pending'` to `WHERE status IN ('active', 'pending')`. For OR across different columns, consider UNION ALL of two simpler queries.

6. Apply window functions to replace self-joins or correlated subqueries. Use `ROW_NUMBER() OVER (PARTITION BY ... ORDER BY ...)` for top-N-per-group queries instead of `GROUP BY` with subqueries.

7. Leverage CTEs (Common Table Expressions) for readability but be aware that PostgreSQL versions before 12 materialize all CTEs. For performance-critical queries on older PostgreSQL, inline the CTE as a subquery.

8. Optimize aggregation queries by filtering before grouping (`WHERE` is more efficient than `HAVING` for non-aggregate conditions), using partial indexes for filtered aggregates, and considering materialized views for expensive recurring aggregations.

9. Test the rewritten query with `EXPLAIN ANALYZE` and compare execution time, row estimates vs. actuals, and buffer usage against the original. The optimized version should show fewer rows processed, index scans replacing sequential scans, and lower total execution time.

10. Document each change made, the reason for the change, and the measured impact so the development team understands and can apply similar patterns to future queries.

## Output

- **Optimized SQL query** with comments explaining each structural change
- **Before/after execution plans** showing performance improvement
- **Index recommendations** (CREATE INDEX statements) needed to support the optimized query
- **Anti-pattern report** listing issues found in the original query with explanations
- **Performance metrics comparison** (execution time, rows scanned, buffer hits)

## Error Handling

| Error | Cause | Solution |
|-------|-------|---------|
| Rewritten query returns different results | JOIN type change (INNER vs LEFT) or NULL handling difference | Verify result sets match with `EXCEPT` query; preserve original JOIN types; handle NULLs explicitly with `COALESCE` |
| Optimized query slower than original | Statistics outdated causing planner to choose wrong plan | Run `ANALYZE` on involved tables; compare `estimated rows` vs `actual rows` in EXPLAIN; consider `SET enable_seqscan = off` to test alternative plans |
| CTE materialization hurting performance | PostgreSQL <12 materializes CTEs preventing predicate pushdown | Inline the CTE as a subquery; upgrade PostgreSQL; add `AS NOT MATERIALIZED` hint in PostgreSQL 12+ |
| Window function query uses excessive memory | Large partition sizes with ORDER BY in window specification | Add `LIMIT` to outer query; use index matching the PARTITION BY and ORDER BY columns; increase `work_mem` for the session |
| UNION ALL produces duplicates | Overlapping conditions in constituent queries | Add mutually exclusive WHERE conditions to each branch; or use UNION (with dedup cost) if overlap is unavoidable |

## Examples

**Converting correlated subquery to JOIN**: Original: `SELECT * FROM orders WHERE customer_id IN (SELECT id FROM customers WHERE region = 'US')` taking 8 seconds with sequential scan on orders. Rewrite: `SELECT o.* FROM orders o JOIN customers c ON o.customer_id = c.id WHERE c.region = 'US'` using index on `orders.customer_id` reduces to 120ms.

**Top-N per group with window function**: Original uses self-join to find the 3 most recent orders per customer (15 seconds). Rewrite: `SELECT * FROM (SELECT *, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY created_at DESC) AS rn FROM orders) sub WHERE rn <= 3` with index on `(customer_id, created_at DESC)` completes in 400ms.

**Eliminating DISTINCT from incorrect JOIN**: `SELECT DISTINCT o.* FROM orders o JOIN line_items li ON o.id = li.order_id WHERE li.amount > 100` scans all line items. Rewrite: `SELECT o.* FROM orders o WHERE EXISTS (SELECT 1 FROM line_items li WHERE li.order_id = o.id AND li.amount > 100)` eliminates the deduplication step and halves execution time.

## Resources

- PostgreSQL query planning: https://www.postgresql.org/docs/current/planner-optimizer.html
- MySQL query optimization: https://dev.mysql.com/doc/refman/8.0/en/optimization.html
- SQL anti-patterns reference: https://use-the-index-luke.com/sql/where-clause
- Window functions tutorial: https://www.postgresql.org/docs/current/tutorial-window.html
- Modern SQL features: https://modern-sql.com/

Related Skills

optimizing-staking-rewards

25
from ComeOnOliver/skillshub

Compare and optimize staking rewards across validators, protocols, and blockchains with risk assessment. Use when analyzing staking opportunities, comparing validators, calculating staking rewards, or optimizing PoS yields. Trigger with phrases like "optimize staking", "compare staking", "best staking APY", "liquid staking", "validator comparison", "staking rewards", or "ETH staking options".

optimizing-prompts

25
from ComeOnOliver/skillshub

Execute this skill optimizes prompts for large language models (llms) to reduce token usage, lower costs, and improve performance. it analyzes the prompt, identifies areas for simplification and redundancy removal, and rewrites the prompt to be more conci... Use when optimizing performance. Trigger with phrases like 'optimize', 'performance', or 'speed up'.

optimizing-gas-fees

25
from ComeOnOliver/skillshub

Optimize blockchain gas costs by analyzing prices, patterns, and timing. Use when checking gas prices, estimating costs, or finding optimal windows. Trigger with phrases like "gas prices", "optimize gas", "transaction cost", "when to transact".

optimizing-database-connection-pooling

25
from ComeOnOliver/skillshub

Process use when you need to work with connection management. This skill provides connection pooling and management with comprehensive guidance and automation. Trigger with phrases like "manage connections", "configure pooling", or "optimize connection usage".

optimizing-cloud-costs

25
from ComeOnOliver/skillshub

Execute use when you need to work with cloud cost optimization. This skill provides cost analysis and optimization with comprehensive guidance and automation. Trigger with phrases like "optimize costs", "analyze spending", or "reduce costs".

optimizing-cache-performance

25
from ComeOnOliver/skillshub

Execute this skill enables AI assistant to analyze and improve application caching strategies. it optimizes cache hit rates, ttl configurations, cache key design, and invalidation strategies. use this skill when the user requests to "optimize cache performance"... Use when optimizing performance. Trigger with phrases like 'optimize', 'performance', or 'speed up'.

optimizing-deep-learning-models

25
from ComeOnOliver/skillshub

This skill optimizes deep learning models using various techniques. It is triggered when the user requests improvements to model performance, such as increasing accuracy, reducing training time, or minimizing resource consumption. The skill leverages advanced optimization algorithms like Adam, SGD, and learning rate scheduling. It analyzes the existing model architecture, training data, and performance metrics to identify areas for enhancement. The skill then automatically applies appropriate optimization strategies and generates optimized code. Use this skill when the user mentions "optimize deep learning model", "improve model accuracy", "reduce training time", or "optimize learning rate".

when-optimizing-prompts-use-prompt-optimization-analyzer

25
from ComeOnOliver/skillshub

Active diagnostic tool for analyzing prompt quality, detecting anti-patterns, identifying token waste, and providing optimization recommendations

when-optimizing-prompts-use-prompt-architect

25
from ComeOnOliver/skillshub

Comprehensive framework for analyzing, creating, and refining prompts for AI systems using evidence-based techniques

when-optimizing-agent-learning-use-reasoningbank-intelligence

25
from ComeOnOliver/skillshub

Implement adaptive learning with ReasoningBank for pattern recognition, strategy optimization, and continuous improvement

backend-queries

25
from ComeOnOliver/skillshub

Write secure, performant, and optimized database queries using parameterized queries, eager loading, proper indexing, and transaction management. Use this skill when writing database queries in controllers, repositories, services, or model methods, when using query builders or ORM methods, when implementing filtering/sorting/pagination logic, when optimizing N+1 query problems with eager loading, when working with joins and complex queries, when implementing query caching, or when wrapping related operations in database transactions.

sql-queries

25
from ComeOnOliver/skillshub

Expert SQL query generation for DBX Studio. Use when writing, optimizing, or debugging SQL queries against user database connections.