query-optimization

Optimize SQLAlchemy PostgreSQL queries with EXPLAIN ANALYZE, indexes, and eager loading.

2|Updated Nov 13, 2025
One-click install
npx skills add https://github.com/ricardoroche/ricardos-claude-code --skill query-optimization
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: query-optimization
Source: https://github.com/ricardoroche/ricardos-claude-code/tree/main/.claude/skills/query-optimization
Command: npx skills add https://github.com/ricardoroche/ricardos-claude-code --skill query-optimization

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill requires sqlalchemy.

What problem does it solve?

This Skill provides a systematic approach to optimizing database queries, addressing issues like slow response times, inefficient data access, and resource contention. It helps developers identify and resolve performance bottlenecks, ensuring applications remain fast and scalable.

Core Features & Use Cases

  • EXPLAIN Analysis: Guides on using EXPLAIN ANALYZE to understand query execution plans and identify performance hotspots.
  • Index Creation: Provides patterns for creating effective single-column, composite, partial, and expression indexes to accelerate data retrieval.
  • N+1 Query Prevention: Explains and solves the N+1 problem using SQLAlchemy's joinedload and selectinload for eager loading.
  • Query Performance Monitoring: Shows how to leverage database statistics (e.g., pg_stat_statements) to find and prioritize slow queries.
  • Efficient SQL Patterns: Guides on writing optimized SQL for SELECT, JOIN, WHERE, and ORDER BY clauses.
  • Use Case: A backend developer notices that a page loading user orders is very slow. This skill helps them use EXPLAIN ANALYZE to inspect the query, identify an N+1 problem when fetching related user data, and then apply joinedload to resolve it, drastically improving page load times.

Quick Start

Analyze the SQL query SELECT * FROM users WHERE email = '[email protected]' using EXPLAIN ANALYZE to understand its execution plan.

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 database queries in SQLAlchemy?

Optimize database queries by using EXPLAIN ANALYZE to inspect execution plans, creating targeted indexes (single-column, composite, partial, or expression), and applying eager loading with joinedload or selectinload to prevent N+1 problems. Start by analyzing your slowest queries with EXPLAIN to identify bottlenecks.

What is the N+1 query problem and how do I fix it?

The N+1 problem occurs when fetching a parent record triggers separate database queries for each related child record. Fix it in SQLAlchemy by using joinedload or selectinload to eager-load relationships in a single optimized query instead of N separate ones.

How do I use EXPLAIN ANALYZE to understand query performance?

Run EXPLAIN ANALYZE on your SQL queries to see the execution plan, row counts, and timing data. This reveals which operations consume the most resources—sequential scans, index usage, joins—helping you identify where to add indexes or restructure queries for better performance.

What types of indexes should I create for faster queries?

Create single-column indexes on frequently filtered fields, composite indexes for multi-column WHERE clauses, partial indexes for conditional queries, and expression indexes for computed columns. Use EXPLAIN ANALYZE to confirm each index actually improves query plans.

Can I monitor which queries are slowest in my PostgreSQL database?

Yes, enable pg_stat_statements in PostgreSQL to track query execution statistics and identify slow queries. Review metrics like total time and call count to prioritize which queries to optimize, then apply indexing or query restructuring to the most expensive ones.

Does query optimization work with SQLAlchemy ORM patterns?

Yes, query optimization works directly with SQLAlchemy ORM. Use joinedload and selectinload for eager loading, EXPLAIN ANALYZE on generated SQL, and index creation on your PostgreSQL tables to optimize ORM queries just as you would with raw SQL.