AdvancedBackendFree prompt

PostgreSQL Query Optimization and Indexing Strategy

Database performance diagnosis with slow query analysis and index planning.

Identify and resolve PostgreSQL performance bottlenecks through query plan analysis, strategic index creation, and query refactoring.

At a glance

Access

Free prompt

Open to copy — no account or payment needed.

Prompt objective

Identify and resolve PostgreSQL performance bottlenecks through query plan analysis, strategic index creation, and query refactoring.

Real use case

A mid-sized e-commerce platform has 2.8 million products and 15,000 daily orders. Search pages load in 4.2 seconds, the admin dashboard times out, and monthly sales reports take 45 minutes to generate. The DBA left and no one knows how to optimize.

Customize these fields first

VERSION: 14/15/16PROJECT NAMELIST WITH RECORD COUNTSNUMBERVALUERDS/SUPABASE/SELF-HOSTEDRAMQUERY 1 — e.g., product search with multiple filters

Replace the placeholders with your own context before you run the prompt. That usually improves the first output more than adding more instructions later.

Prompt

Optimize PostgreSQL [VERSION: 14/15/16] database performance for [PROJECT NAME] with the following characteristics:

**Database Context:**
- Main tables: [LIST WITH RECORD COUNTS]
- Write volume: [NUMBER] inserts/updates per hour
- Read volume: [NUMBER] queries per second
- Total size: [VALUE] GB
- Hosting: [RDS/SUPABASE/SELF-HOSTED] with [RAM] memory

**Problematic Queries (paste SQL or describe):**
1) [QUERY 1 — e.g., product search with multiple filters]
2) [QUERY 2 — e.g., aggregate report with JOINs]
3) [QUERY 3 — e.g., real-time dashboard with counts]

**1) EXPLAIN ANALYZE Analysis:**
- For each query, show how to interpret the execution plan
- Identify: unnecessary Seq Scans, expensive Nested Loops, inefficient Hash Joins
- Estimated cost vs. actual cost
- Estimated rows vs. actual rows (variance = outdated statistics)

**2) Indexing Strategy:**
- B-tree indexes for equality and range filters
- GIN indexes for full-text search and arrays/JSONB
- Partial indexes (WHERE condition) for frequent queries
- Composite indexes (column order matters)
- Covering indexes (INCLUDE) to avoid table lookups
- Estimated size of each index
- Trade-off: write impact vs. read gain

**3) Query Refactoring:**
- CTEs vs. subqueries (materialization in PG 14+)
- Using LATERAL JOIN for correlated queries
- Materialized Views for heavy reports (refresh strategy)
- Table partitioning for large tables by date
- Connection pooling with PgBouncer

**4) PostgreSQL Configuration:**
- shared_buffers, work_mem, effective_cache_size
- max_connections vs. connection pooling
- Autovacuum tuning for tables with heavy UPDATE/DELETE
- pg_stat_statements for continuous monitoring

For each optimization, estimate the expected performance gain (e.g., from 4.2s to < 200ms).

Open directly in an AI — the text is pre-filled:

How to use this prompt

  1. 1Replace the key placeholders first: VERSION: 14/15/16, PROJECT NAME, LIST WITH RECORD COUNTS, NUMBER.
  2. 2Replace any bracketed placeholders like [this] with your own context.
  3. 3Add extra background information when you want more tailored results.
  4. 4Combine multiple prompts in one conversation when you need a richer output.
  5. 5Save your best-performing prompts so they are easy to reuse later.

Next best step

Open the guide first, then branch only if you still need more.

A guide for technical builders choosing between prompts, coding workflows, and agent-based implementation.

If this prompt is close but not quite right, generate variants next. If the job is recurring, move into the course library after the guide.

Related prompts

View all

Explore other prompt categories

Move sideways into adjacent libraries when the current category is not the full answer.

Every prompt here is free. The course teaches the thinking behind them.

Copy as many prompts as you like. When you want to move from single prompts to a repeatable AI workflow, Learn AI in 30 Days walks through it, one day at a time.

Get the courseSee the 30-day curriculum first

Buy the course once ($15/$20 by length), or go all-access for $10/mo with a verifiable certificate.