sql-optimization-patterns

Optimizes slow SQL queries through indexing strategies, EXPLAIN plan analysis, and query rewriting patterns.

Updated Aug 11, 2026
One-click install
npx skills add https://github.com/DucCuong159/Realtime-chatapp --skill sql-optimization-patterns-duccuong159
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sql-optimization-patterns
Source: https://github.com/DucCuong159/Realtime-chatapp/tree/main/.agent/skills/sql-optimization-patterns
Command: npx skills add https://github.com/DucCuong159/Realtime-chatapp --skill sql-optimization-patterns-duccuong159

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve? Slow database queries degrade application performance and increase infrastructure costs. This Skill provides systematic patterns for diagnosing bottlenecks, designing indexes, and rewriting inefficient queries in PostgreSQL and MySQL. ## Core Features & Use Cases - Query Plan Analysis: Interpret EXPLAIN and EXPLAIN ANALYZE output to identify sequential scans, costly joins, and estimation errors. - Index Design Patterns: Apply B-Tree, GIN, partial, covering, and composite indexes matched to actual query shapes. - Anti-Pattern Fixes: Eliminate N+1 queries, replace OFFSET pagination with cursor-based keyset pagination, and batch INSERT/UPDATE operations. - Use Case: A dashboard endpoint takes 8 seconds because of a correlated subquery counting orders per user. Use this Skill to rewrite it as a JOIN with aggregation, add a composite index on (user_id, status), and verify the improvement with EXPLAIN ANALYZE. ## Quick Start Analyze this slow query and its EXPLAIN output, then recommend indexes and a rewritten version that avoids the sequential scan.

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 to see the actual execution plan, then look for sequential scans on large tables and costly join operations. Add targeted indexes matching your WHERE and JOIN columns, and rewrite the query to filter rows before joining.

How to fix N+1 query problems in database applications?

Replace per-row queries with a single JOIN or a batched query using WHERE id IN (...). Load the parent records first, collect their IDs, then fetch all related records in one query and group them in application code.

What is cursor-based pagination and why is it faster than OFFSET?

Cursor pagination filters rows with WHERE created_at < last_seen_value instead of skipping rows with OFFSET. OFFSET forces the database to scan and discard all preceding rows, while a cursor uses an index to jump directly to the starting point.

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

Common causes include functions wrapping the column in WHERE clauses, implicit type conversions, stale table statistics, or the planner estimating a sequential scan as cheaper for small tables. Run ANALYZE to refresh statistics and check for expression mismatches.

When should I use a materialized view instead of a regular query?

Use materialized views for expensive aggregations queried repeatedly, such as per-user order summaries. They pre-compute results for fast reads but require periodic REFRESH, so they fit reporting workloads rather than real-time data.

What are the downsides of adding too many indexes?

Every index slows down INSERT, UPDATE, and DELETE operations because each write must also update the index structures. Unused indexes waste storage, so monitor pg_stat_user_indexes and drop indexes with zero scans.