sql-optimization-patterns

Analyze EXPLAIN plans and refactor slow SQL queries for PostgreSQL and MySQL.

2|Updated Nov 21, 2025
One-click install
npx skills add https://github.com/maxjuniorbr/segflow-crm --skill sql-optimization-patterns-maxjuniorbr
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: sql-optimization-patterns
Source: https://github.com/maxjuniorbr/segflow-crm/tree/main/.cursor/skills/sql-optimization-patterns
Command: npx skills add https://github.com/maxjuniorbr/segflow-crm --skill sql-optimization-patterns-maxjuniorbr

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill provides a structured approach to diagnosing and resolving slow SQL queries by focusing on execution plans, indexing, and query refactoring to reduce latency in relational databases.

Core Features & Use Cases

  • Analyze EXPLAIN plans to identify bottlenecks such as sequential scans and inefficient joins.
  • Design and tune indexes (including partial, composite, and expression-based indexes) to accelerate common queries.
  • Refactor queries to avoid anti-patterns like N+1 queries, large sorts, and non-sargable predicates, enabling faster data access and better scalability.
  • Apply practical patterns for pagination, aggregation, and join optimization across PostgreSQL and MySQL workloads.

Quick Start

Run EXPLAIN ANALYZE on a slow query to identify bottlenecks and apply proper indexes.

Frequently Asked Questions about sql-optimization-patterns

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

FAQPage Schema
When should I avoid using expression-based indexes for query optimization?

Avoid expression-based indexes when simpler composite or partial indexes suffice for your execution plan, as expression indexes add write overhead and may be unnecessary for resolving common slow query bottlenecks.

How do I optimize slow SQL queries using execution plans?

To optimize slow SQL queries, run EXPLAIN ANALYZE to identify bottlenecks like sequential scans and inefficient joins, then apply proper indexes or refactor predicates to reduce latency in production workloads.

What is the best way to fix N+1 queries and full table scans in PostgreSQL?

Fixing N+1 queries and full table scans in PostgreSQL involves refactoring non-sargable predicates and designing composite or partial indexes to accelerate data access and improve scalability.

How do I design effective indexes for query tuning in relational databases?

Design effective indexes for query tuning by analyzing execution plans and creating partial, composite, or expression-based indexes that directly target slow joins, aggregations, and pagination patterns.

Does this approach work for both PostgreSQL and MySQL workloads?

Yes, this approach works for both PostgreSQL and MySQL workloads, applying practical patterns for pagination, aggregation, and join optimization across relational databases to reduce latency.

Why does my SQL query still have high latency after adding a basic index?

Your SQL query latency remains high because basic indexes may not resolve inefficient joins, large sorts, or non-sargable predicates; you must analyze execution plans and apply composite or expression-based indexes.