query-optimization

Diagnose slow SQL queries and N+1 patterns using EXPLAIN ANALYZE and pg_stat_statements.

Updated Mar 29, 2026
One-click install
npx skills add https://github.com/marquesfelip/agents-and-skills --skill query-optimization-marquesfelip
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: query-optimization
Source: https://github.com/marquesfelip/agents-and-skills/tree/main/skills/query-optimization
Command: npx skills add https://github.com/marquesfelip/agents-and-skills --skill query-optimization-marquesfelip

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

Relational database performance suffers from slow queries, N+1 ORM patterns, and inefficient joins and pagination, leading to higher latency and database load.

Core Features & Use Cases

  • Diagnose slow queries using EXPLAIN ANALYZE, pg_stat_statements, and ORM traces to identify bottlenecks.
  • Propose actionable optimizations including indexing strategies, join rewrites, pagination techniques (keyset vs offset), and smarter CTE usage.
  • Apply across PostgreSQL and MySQL environments, covering both single-tenant and high-traffic workloads, with guidance for ORM-driven apps.

Quick Start

Analyze a provided slow query by running EXPLAIN ANALYZE and propose concrete optimizations.

Frequently Asked Questions about query-optimization

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

FAQPage Schema
How do I optimize slow SQL queries using EXPLAIN ANALYZE?

To optimize slow SQL queries, run EXPLAIN ANALYZE to identify execution bottlenecks and then apply targeted indexing or join rewrites. This process pinpoints inefficiencies in relational database plans to reduce latency and database load.

What is the best way to fix N+1 query patterns in an ORM?

Fixing N+1 query patterns involves analyzing ORM traces to detect redundant database calls and applying application-level optimization patterns. Resolving these patterns drastically reduces relational database load and query latency.

How do I handle large pagination queries without slowing down the database?

Handle large pagination queries by switching from offset to keyset pagination strategies. Keyset pagination maintains performance during deep pagination by avoiding full table scans that offset techniques trigger in high-traffic workloads.

Does this SQL optimization approach work for both PostgreSQL and MySQL?

Yes, this SQL optimization approach works for both PostgreSQL and MySQL environments. It provides tailored index suggestions, join rewrites, and CTE guidance applicable across single-tenant and high-traffic relational database workloads.

When should I use pg_stat_statements to diagnose query bottlenecks?

Use pg_stat_statements to diagnose query bottlenecks when you need to identify slow execution patterns across aggregated database traffic. This complements EXPLAIN ANALYZE by highlighting frequently run queries causing high latency.

Why are my complex joins causing high database latency?

Complex joins cause high database latency when missing indexes force full table scans or inefficient execution plans. Diagnose the join structure with EXPLAIN ANALYZE and apply join rewrites to optimize relational database performance.