query-optimization

Optimize slow PostgreSQL queries using Drizzle ORM with indexes and joins.

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

SYSTEM DOCUMENTATION & REQUIREMENTS

What problem does it solve?

This Skill helps optimize database queries using Drizzle ORM and PostgreSQL, addressing slow queries, missing indexes, and N+1 problems.

Core Features & Use Cases

  • Indexing & Patterns: Add indexes for frequently queried columns and optimize WHERE clauses.
  • Query Optimizations: Suggest joins over subqueries and pagination strategies.
  • Monitoring: Log and profile query timings to identify bottlenecks.

Quick Start

Identify a slow query, add an index, and validate performance with a timed query example.

Frequently Asked Questions about query-optimization

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

FAQPage Schema
How do I speed up slow database queries in PostgreSQL?

Slow queries often result from missing indexes, N+1 problems, or inefficient joins. Add indexes on frequently queried columns, use query logging to identify bottlenecks, and replace subqueries with joins to improve performance measurably.

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

An N+1 problem occurs when fetching one record triggers N additional queries for related data. Fix it by using joins or batch operations in Drizzle ORM to load all data in one or two queries instead of many separate calls.

How do I add indexes to PostgreSQL tables with Drizzle ORM?

Define indexes on columns used frequently in WHERE clauses and joins within your Drizzle schema. Run migrations to apply them to your database, then validate performance improvements using query timing logs.

Can I use pagination to improve query performance?

Yes. Pagination with LIMIT and OFFSET reduces memory usage and network overhead by fetching smaller result sets. Combine it with indexes on sort columns to maintain fast query execution across large datasets.

How do I identify which queries are bottlenecks in my application?

Enable query logging in PostgreSQL to capture execution time and frequency. Profile slow queries using EXPLAIN ANALYZE, then prioritize optimization on the most frequently executed or slowest queries.

Does Drizzle ORM support selective column retrieval to reduce query overhead?

Yes. Drizzle ORM lets you select only the columns you need instead of fetching entire rows, reducing data transfer and improving query speed, especially on tables with many columns or large datasets.