database-query-optimizer

Optimize Django ORM queries for PostgreSQL to resolve slow API responses.

Updated Sep 22, 2025
One-click install
npx skills add https://github.com/allthriveai/allthriveai --skill database-query-optimizer-allthriveai
Or copy as Structured Prompt for Agent
Please help me install this Agent Skill.
Skill: database-query-optimizer
Source: https://github.com/allthriveai/allthriveai/tree/main/.claude/skills/database-query-optimizer
Command: npx skills add https://github.com/allthriveai/allthriveai --skill database-query-optimizer-allthriveai

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps you analyze and optimize Django ORM queries and database performance, addressing slow queries, N+1 patterns, missing indexes, and migration issues.

Core Features & Use Cases

  • N+1 Detection & Remediation: Identify N+1 query patterns and apply select_related or prefetch_related.
  • Indexing & Query Optimization: Recommend indexes and efficient field lookups to speed filters.
  • Migration & Schema Review: Analyze migrations for performance pitfalls and index usage.
  • Query Explain & Logging: Enable explain plans and log queries to diagnose bottlenecks.

Quick Start

Profile a slow endpoint, review executed SQL, enable query logging, and apply optimizations like select_related/prefetch_related and indexing. Re-run to verify performance improvements.

Frequently Asked Questions about database-query-optimizer

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

FAQPage Schema
How do I fix N+1 query problems in Django ORM?

N+1 queries occur when Django executes one initial query plus additional queries for each related object. Use select_related() for foreign keys and prefetch_related() for many-to-many and reverse relationships to load all data in two queries instead of N+1, significantly reducing database calls and API response time.

Why are my Django API endpoints slow?

Slow endpoints typically result from inefficient queryset evaluation, missing database indexes, or N+1 patterns. Enable query logging to inspect executed SQL, identify bottlenecks, add indexes on frequently filtered fields, and apply ORM optimizations like select_related, prefetch_related, and values() to retrieve only needed columns.

How do I optimize Django queries with indexes and field selection?

Create database indexes on fields used in WHERE clauses and joins to speed lookups. Use only() and defer() to limit retrieved columns, and values() and values_list() for aggregations. Review Django migrations to ensure indexes are applied, then verify improvements by comparing query execution plans before and after optimization.

Can I debug Django ORM queries in production environments?

Yes. Enable query logging and explain plans to capture SQL execution details in production and Dockerized environments. Log query counts and execution time per endpoint, use Django Debug Toolbar in development, and integrate monitoring into CI pipelines to catch performance regressions in API endpoints and admin views.

What database issues should I check in Django migrations?

Review migrations for missing indexes, inefficient schema changes, and performance pitfalls introduced during schema evolution. Analyze migration files for index definitions, field constraints, and PostgreSQL-specific features to ensure the database schema supports optimized query plans and prevents slow queries after deployment.