sql-optimization

Optimizes slow SQL queries through indexing, query rewriting, and execution plan analysis.

1|Updated Mar 21, 2026
One-click install
npx skills add https://github.com/kalilurrahman/kr-claudiator-skills-original-prompts --skill sql-optimization-kalilurrahman
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sql-optimization
Source: https://github.com/kalilurrahman/kr-claudiator-skills-original-prompts/tree/main/03-data-analytics/sql-optimization
Command: npx skills add https://github.com/kalilurrahman/kr-claudiator-skills-original-prompts --skill sql-optimization-kalilurrahman

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Slow database queries degrade application performance, and fixing them without a systematic approach leads to guesswork. This Skill provides a structured method to diagnose slow queries using EXPLAIN ANALYZE, apply the right index strategies, and measure concrete before/after improvements. ## Core Features & Use Cases - Anti-Pattern Detection: Identifies and fixes common issues like missing indexes, SELECT *, N+1 queries, OR conditions, functions on indexed columns, and leading-wildcard LIKE searches. - Index Strategy Design: Covers composite, covering, partial, and expression indexes with correct column ordering rules. - Query Rewriting & Execution Plans: Rewrites subqueries as JOINs, limits before joins, reads EXPLAIN ANALYZE output, and uses materialized views or partitioning for large tables. - Use Case: A PostgreSQL orders table with 5M rows takes 8.5 seconds per lookup. The Skill diagnoses a sequential scan, adds a composite index, rewrites the query, and verifies the drop to 45 milliseconds. ## Quick Start Analyze this slow PostgreSQL query on my orders table and recommend indexes and a rewritten version with before/after EXPLAIN ANALYZE results.

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 in PostgreSQL?

Start by running EXPLAIN ANALYZE to see the actual execution plan and identify sequential scans or bad row estimates. Then add indexes on WHERE clause columns, rewrite anti-patterns like SELECT * or OR conditions, and run ANALYZE to update table statistics before measuring again.

How do I fix N+1 query problems in Django ORM?

Use select_related for foreign key relationships or prefetch_related for many-to-many relations to fetch related data in a single JOIN query. This reduces 1001 queries down to 1, eliminating per-row roundtrips to the database.

What column order should a composite index use?

Place equality condition columns first, range conditions second, and ORDER BY columns last, with the most selective column leading. For example, index (user_id, status, created_at DESC) for a query filtering by user and status then sorting by date.

Why is my index not being used by the query planner?

Common causes include applying functions to indexed columns like LOWER(email), leading wildcards in LIKE patterns, or stale table statistics. Fix these with functional or trigram indexes, full-text search, or running ANALYZE to refresh planner statistics.

When should I use a materialized view instead of an index?

Use materialized views for expensive aggregations queried repeatedly, such as daily revenue rollups over millions of rows. They precompute results for instant reads and can be refreshed periodically with REFRESH MATERIALIZED VIEW CONCURRENTLY.

When should a table be partitioned in PostgreSQL?

Partition tables exceeding roughly 100 million rows, typically by date range, so queries only scan relevant partitions instead of the entire table. Each partition gets its own indexes, which also keeps index sizes smaller and maintenance faster.