sql-optimization-patterns

Optimize slow SQL queries by analyzing EXPLAIN plans and recommending indexes.

Updated Mar 18, 2026
One-click install
npx skills add https://github.com/kyamsSRL/TinyCopro --skill sql-optimization-patterns-kyamssrl
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sql-optimization-patterns
Source: https://github.com/kyamsSRL/TinyCopro/tree/main/.claude/skills/sql-optimization-patterns
Command: npx skills add https://github.com/kyamsSRL/TinyCopro --skill sql-optimization-patterns-kyamssrl

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill tackles slow database queries, high latency, and inefficient schema patterns by providing systematic guidance to diagnose performance bottlenecks and apply proven optimizations.

Core Features & Use Cases

  • EXPLAIN plan analysis: Interpret execution plans to identify sequential scans, costly joins, and index usage issues.
  • Indexing strategies: Recommend B-Tree, GIN, GiST, BRIN, functional, partial, and covering indexes tailored to query patterns.
  • Query refactors & patterns: Eliminate N+1 problems, convert correlated subqueries, optimize pagination with cursors, and suggest batch operations.
  • Advanced techniques: Advise on materialized views, partitioning strategies, and operational maintenance (VACUUM, ANALYZE, reindexing).
  • Use Case: Improve a reporting query that scans large tables by adding appropriate indexes, rewriting joins, and suggesting partitioning or materialized views for repeated reports.

Quick Start

Analyze the provided slow PostgreSQL query and EXPLAIN ANALYZE output and return a rewritten query, recommended indexes and partitioning options, and an explanation of expected performance improvements.

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 slow PostgreSQL queries using EXPLAIN plan analysis?

To optimize slow PostgreSQL queries, analyze the EXPLAIN plan output to identify sequential scans and costly joins, then apply concrete query rewrites and tailored index recommendations to reduce execution time.

What is the best way to fix N+1 query problems in relational databases?

The best way to fix N+1 query problems is to refactor the query logic by converting correlated subqueries and suggesting batch operations, eliminating redundant sequential scans and reducing overall database latency.

How do I choose between B-Tree, GIN, and partial indexes for database performance?

Choosing between B-Tree, GIN, and partial indexes depends on your query patterns; the Skill recommends specific indexing strategies tailored to your execution plans to balance read performance with write cost and maintenance.

When should I use materialized views or partitioning for query optimization?

You should use materialized views or partitioning for query optimization when reporting queries scan large tables repeatedly, allowing you to cache complex joins and divide large datasets for faster data retrieval.

Does this query optimization approach work with relational databases other than PostgreSQL?

Yes, while the query optimization approach focuses on PostgreSQL features like VACUUM and ANALYZE, the underlying principles for index strategies and refactoring N+1 patterns apply to other relational databases.

Why do my SQL queries still have high latency after adding database indexes?

SQL queries maintain high latency after adding indexes if the execution plan shows inefficient joins or pagination patterns; resolving this requires query rewrites like cursor-based pagination and operational maintenance such as reindexing.