sql-optimization

Diagnose and optimize SQL queries using EXPLAIN ANALYZE, indexing, and pagination patterns.

Updated Dec 29, 2025
One-click install
npx skills add https://github.com/snoodleboot-io/discrecontinual_equations --skill sql-optimization-snoodleboot-io
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: sql-optimization
Source: https://github.com/snoodleboot-io/discrecontinual_equations/tree/main/.claude/skills/sql-optimization
Command: npx skills add https://github.com/snoodleboot-io/discrecontinual_equations --skill sql-optimization-snoodleboot-io

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Slow database queries and inefficient SQL patterns degrade application performance, and developers often lack a systematic checklist for diagnosing bottlenecks like sequential scans, N+1 queries, and missing indexes. ## Core Features & Use Cases - Execution Plan Analysis: Start every optimization with EXPLAIN ANALYZE to identify Seq Scans, Index Scans, and inefficient filter operations. - Index Strategy Guidance: Create single-column, multi-column, partial, and expression indexes matched to query patterns. - Anti-Pattern Detection: Fix N+1 queries with joins, replace OFFSET pagination with keyset pagination, avoid SELECT *, and eliminate functions in WHERE clauses that block index usage. - Use Case: A developer notices an orders endpoint timing out; use this Skill to analyze the query plan, add a composite index on created_at and status, and rewrite the pagination to use keyset cursors. ## Quick Start Analyze this slow SQL query and suggest indexes and rewrites to make it faster.

Frequently Asked Questions about sql-optimization

High-intent search queries and answers about installing and using this skill.

FAQPage Schema
How do I optimize a slow SQL query?▼

Start by running EXPLAIN ANALYZE on the query to see the actual execution plan. Look for Seq Scans on large tables, then add indexes on filtered columns, remove functions from WHERE clauses, and fetch only the columns you need.

How to fix N+1 query problems in database code?▼

Replace per-row queries inside loops with a single query using a JOIN. For example, instead of querying customers once per order, join orders and customers in one SELECT statement to fetch all data at once.

Why is OFFSET pagination slow on large tables?▼

OFFSET forces the database to scan and discard all skipped rows before returning results, so cost grows with page depth. Keyset pagination using WHERE id > last_seen_id with LIMIT uses indexes and stays fast at any depth.

When should I use a partial index in SQL?▼

Use a partial index when queries consistently filter on the same condition, such as status IN ('pending', 'processing'). It indexes only matching rows, making the index smaller and faster than a full-table index.

Why can't my query use an index on a date column?▼

Wrapping the column in a function like YEAR(created_at) prevents index usage because the database must compute the function for every row. Rewrite it as a range condition, such as created_at >= '2026-01-01' AND created_at < '2027-01-01'.