Works with the AI tools you already use

    Claude CodeClaude CodeCursorCursorCodex CLICodex CLIMuseMuseDotsDots+22 more

    sql optimization patterns

    1

    Optimize SQL performance through EXPLAIN plan analysis, index design, and query refactoring.

    $8

    Secure checkout via Stripe

    30-day refund guarantee

    Converts to your local currency at checkout

    Security scanned
    sql optimization patterns

    sql optimization patterns

    Example session with this skill installed

    I have a slow query on PostgreSQL. Table 'orders' has 2M rows. Query: SELECT * FROM orders WHERE status = 'pending' AND created_at > '2023-01-01' ORDER BY created_at DESC. Here is the EXPLAIN plan.

    • Read your context and instructions
    • Compiled the sql optimization patterns
    • Generated the document

    The analysis indicates a Parallel Seq Scan on the orders table because the current index on created_at is not being utilized efficiently alongside the status filter. The query cost is high due to the filter operation occurring after the disk read.

    SQL Optimization Report

    ComponentFindingRecommendation
    Current PlanParallel Seq Scan on orders (cost=0.00..45823.10)The planner is ignoring the single-column index on created_at due to the status filter.
    BottleneckFilter: (status = 'pending'::text)85% of execution time is spent filtering rows in memory after a full table scan.
    Index StrategyMissing Composite IndexCreate a composite index covering both the filter and sort columns to allow an Index Scan.
    Query RewriteSelectivity IssuesReplace SELECT * with specific columns to reduce memory overhead and potentially allow an Index Only Scan.

    Proposed Index

    CREATE INDEX idx_orders_status_created_at_desc 
    ON orders (status, created_at DESC);
    

    Performance Validation Plan

    1. Baseline: Run EXPLAIN ANALYZE on the current query three times to establish a median execution time.
    2. Apply Change: Execute the CREATE INDEX statement (preferably CONCURRENTLY in production).

    Verify: Run EXPLAIN ANALYZE again. Expected result is an Index Scan or Index Only Scan using idx_orders_status_created_at_desc.
    4. Rollback: DROP INDEX idx_orders_status_created_at_desc; if execution time does not decrease by at least 40%.

    Next steps

    • Provide the output of \d orders to confirm there are no conflicting indexes.
    • Run the baseline EXPLAIN ANALYZE and share the total execution time in milliseconds.
    • Confirm if this table receives high write volume, as additional indexes impact INSERT performance.

    sql-optimization-patterns.pdf

    PDF · document

    Generated

    Example file from a real run - the skill writes it into your workspace.

    Connects securely to your tools. The creator never sees your data.

    What you get

    Identify bottlenecks in EXPLAIN plans to reduce query execution time.Design composite and partial indexes to optimize specific search predicates.Detect and fix N+1 query patterns in application code.Create structured validation plans for database schema changes.

    About this skill

    The problem

    Slow SQL queries drag down application performance and increase cloud costs. Identifying the exact cause in an EXPLAIN plan or designing the correct composite index is time-consuming and prone to error.

    What it does

    • Analyzes EXPLAIN and EXPLAIN ANALYZE output to identify bottleneck operations like sequential scans or costly joins.
    • Audits existing indexing strategies and proposes specific composite, partial, or functional indexes based on query predicates.
    • Detects N+1 query patterns in application code and suggests batching or join-based rewrites.
    • Provides structured optimization reports including a baseline measurement, proposed changes, and a validation plan.

    Frameworks & tools

    PostgreSQL, MySQL, MariaDB, and SQL Server. Compatible with ORMs like Eloquent, TypeORM, and SQLAlchemy for identifying N+1 issues.

    Why this beats prompting it yourself

    Generic LLM prompts often suggest "add an index" without checking existing constraints or table cardinality. This skill enforces a strict evidence-based workflow, requiring plan analysis before making recommendations to avoid redundant indexes and production regression.

    Use cases

    • Triaging slow queries reported in APM tools like New Relic or Datadog.
    • Reviewing schema migrations to ensure new features have appropriate indexing from day one.
    • Refactoring legacy SQL queries that have slowed down as table volume reached millions of rows.

    Known limitations

    Requires table schema or cardinality information to provide accurate cost estimates. Cannot execute changes directly on production databases.

    How to install

    Works the same in every agent - Claude, Cursor, Codex, Copilot and 20+ more.

    ~30 seconds
    1. 1

      Download the ZIP

      Free skills download straight away. Paid skills unlock right after purchase.

    2. 2

      Unzip into your skills folder

      Every agent reads skills from one folder on your machine. Drop the unzipped folder in there.

    3. 3

      Ask your agent to use it

      Restart the agent if it was already running. It picks the skill up automatically - no config needed.

    Skills folder by agent

    Click the path to copy it. Create the folder if it does not exist yet.

    Reviews

    No reviews yet

    Be one of the first to try it. Every listed skill passes our trust checks below.

    Security scanned

    Passed our 8-point scan before listing

    Fresh listing

    Recently published to Agensi

    30-day refund

    Not a fit? Get your money back

    Trust & safety

    Security scanned

    Verified clean 29 days ago

    • Passed all security checks, Safe to install

    Listed29 days ago

    What's inside

    Frequently Asked Questions