database-query-optimization

Diagnose and optimize Drizzle ORM queries, indexes, and N+1 patterns in PostgreSQL.

Updated Dec 24, 2025
One-click install
npx skills add https://github.com/JoyJoin-Tech-Limited/JoyJoin_app_v0.1 --skill database-query-optimization-joyjoin-tech-limited
Or copy as Structured Prompt for Agent▼
Please help me install this Agent Skill.
Skill: database-query-optimization
Source: https://github.com/JoyJoin-Tech-Limited/JoyJoin_app_v0.1/tree/main/.github/skills/database-query-optimization
Command: npx skills add https://github.com/JoyJoin-Tech-Limited/JoyJoin_app_v0.1 --skill database-query-optimization-joyjoin-tech-limited

SYSTEM DOCUMENTATION & REQUIREMENTS

💡 This Skill includes references (resource) components.

What problem does it solve? Slow API routes and inefficient database access are hard to diagnose without clear patterns. This Skill provides concrete guidance for finding N+1 queries, choosing the right indexes, and writing efficient Drizzle ORM queries against PostgreSQL in the JoyJoin server. ## Core Features & Use Cases - N+1 Detection and Batching: Replace per-row queries inside loops with inArray batch loading and in-memory Map assembly, following the canonical preloadUserInterests pattern. - Index Strategy Guidance: Decide when to add single-column, composite, partial, or covering indexes, with rules for column ordering by selectivity. - Query Review Checklist: Enforce narrow column projections, correct innerJoin/leftJoin usage, and proper transaction scoping with tx: DatabaseLike. - Use Case: A route returning event participants is slow. Check per-request dbCount metrics from the db proxy, find the loop-based participant lookup, and rewrite it as a single inArray query grouped by event ID. ## Quick Start Ask the AI to review a slow repository method or route for N+1 queries and missing indexes using the database query optimization guidance.

Frequently Asked Questions about database-query-optimization

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

FAQPage Schema
How do I fix N+1 queries in Drizzle ORM?▼

Replace per-row queries inside loops with a single query using inArray over the collected IDs, then group results in memory with a Map keyed by the foreign key. The preloadUserInterests function is the canonical batch-preload example in this codebase.

How do I find slow database queries in an Express API?▼

Check the per-request dbCount, dbMs, and dbMax metrics tracked by the db proxy wrapper. A high dbCount relative to the response payload signals N+1 or redundant queries, and Prometheus histograms at /api/metrics show latency trends.

When should I use a composite index vs a partial index in PostgreSQL?▼

Use a composite index when queries filter on multiple columns, leading with the most selective or equality-filtered column. Use a partial index for status columns with skewed distributions, such as indexing only rows where match_status equals pending.

Does Drizzle automatically create indexes for foreign keys in PostgreSQL?▼

No, Drizzle does not auto-create indexes for foreign keys in PostgreSQL. You should add indexes manually for foreign key columns, especially on high-volume tables like eventAttendance which currently lacks indexes entirely.

When should I not use query optimization patterns?▼

Skip this guidance for schema design, migrations, or backfills, which belong to migration safety workflows. It also does not apply to trivial single-row lookups, seed data, fixtures, or test-only queries where performance is irrelevant.