sql-optimization-patterns

Optimize SQL queries using indexing strategies, EXPLAIN analysis, and query rewriting patterns.

Updated Apr 23, 2026
One-click install
npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill sql-optimization-patterns-sanketadlak
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sql-optimization-patterns
Source: https://github.com/SanketAdlak/PDMProjectDesign/tree/main/.agents/skills/sql-optimization-patterns
Command: npx skills add https://github.com/SanketAdlak/PDMProjectDesign --skill sql-optimization-patterns-sanketadlak

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Slow database queries, missing indexes, and N+1 query patterns degrade application performance and increase infrastructure costs. This Skill provides systematic guidance to diagnose and fix these bottlenecks. ## Core Features & Use Cases - EXPLAIN Plan Analysis: Interpret query execution plans to identify sequential scans, inefficient joins, and costly operations. - Index Strategy Design: Create B-Tree, GIN, partial, covering, and composite indexes matched to query patterns. - Query Rewriting Patterns: Eliminate N+1 queries, replace OFFSET pagination with cursor-based pagination, and optimize aggregations and subqueries. - Use Case: A dashboard endpoint takes 8 seconds to load. Use this Skill to analyze the EXPLAIN output, discover a missing composite index on (user_id, status), and reduce the query to under 100ms. ## Quick Start Analyze this slow PostgreSQL query and recommend indexes and rewrites to improve its performance.

Frequently Asked Questions about sql-optimization-patterns

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 execution plan. Look for sequential scans on large tables, then add appropriate indexes, rewrite functions in WHERE clauses, and select only needed columns.

How to fix N+1 query problems in applications?

Replace per-row queries with a single JOIN or a batch query using WHERE id IN (...). Load related records in one query, then group results by foreign key in application code.

What index type should I use in PostgreSQL?

Use B-Tree for equality and range queries, GIN for full-text search, arrays, and JSONB, and BRIN for very large tables with natural ordering. Partial and covering indexes further reduce size and enable index-only scans.

Why is OFFSET pagination slow on large tables?

OFFSET forces the database to scan and discard all preceding rows, so cost grows with offset size. Cursor-based pagination using WHERE on the last seen key with a matching index keeps performance constant.

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

Common causes include functions wrapping the column in WHERE, implicit type conversion, leading-wildcard LIKE patterns, and stale statistics. Create a functional index, match types exactly, and run ANALYZE to refresh statistics.

When should I use a materialized view?

Use materialized views to pre-compute expensive aggregations or joins that are read frequently but tolerate slightly stale data. Refresh them on a schedule, using CONCURRENTLY in PostgreSQL to avoid blocking reads.