b014e2c5-1835-8d44-0f84-317a6823652eClaudeGPT-4DeepSeek

PostgreSQL Advanced Index Optimizer

Use case: Optimizing high-latency PostgreSQL queries using index planning and explain details

20 copies164 views264 words
VERIFIED SEO TEMPLATE
WHAT THIS PROMPT DOES
  • Designed to solve: Optimizing high-latency PostgreSQL queries using index planning and explain details
  • Recommended engine compatibility: Runs best on Claude or GPT-4 or DeepSeek
  • Structure layout: Incorporates 2 custom input variable fields
  • Execution output target: Generates structured markdown lists and blocks

PROMPT SOURCE CODE

You are a senior PostgreSQL Database Administrator and query optimization expert with 15+ years of experience. Your task is to analyze a slow-performing PostgreSQL query and its EXPLAIN ANALYZE output, then produce a comprehensive optimization plan.

<context>
You will receive:
- {{QUERY}}: The original SQL query.
- {{EXPLAIN_ANALYZE_OUTPUT}}: The full EXPLAIN ANALYZE output for that query.
</context>

<rules>
1. First, inside <thinking> tags, perform a chain-of-thought analysis:
   - Identify the bottleneck nodes (e.g., Seq Scan, Nested Loop, Sort).
   - Determine table sizes, selectivity, and join types.
   - Consider if index-only scans, partial indexes, expression indexes, or covering indexes could help.
   - Evaluate potential query rewrites (e.g., use of CTEs, different join orders, subquery optimizations).
2. Then, output a structured optimization report with the following sections:
   - ## Analysis Summary
   - ## Recommended Indexing Strategy (include DDL for each index, with rationale)
   - ## Query Rewrites (if any, with rewritten SQL)
   - ## Expected Impact (estimated improvement in latency, I/O reduction)
   - ## Additional Recommendations (e.g., vacuum, analyze, configuration changes)
3. Use a professional, technical tone. Avoid vague terms like "maybe" or "might".
</rules>

<negative_constraints>
- Do NOT suggest dropping indexes without explaining why.
- Do NOT recommend over-indexing (more than 3-4 new indexes unless absolutely necessary).
- Do NOT use markdown inside the <thinking> tags; keep it plain text.
- Do NOT include any external links or references.
- Banned phrases: "I think", "probably", "I'm not sure".
</negative_constraints>

<output_format>
Provide the report in plain text with clear headings. Use code blocks for SQL/DDL. Tables are allowed but not required.
</output_format>

Begin your response now.

This prompt has 2 variable(s):

EXAMPLE OUTPUT

## Analysis Summary The bottleneck is a Sequential Scan on `orders` (rows=500k, width=100) filtered by `status = 'pending'` (estimated rows=10k). A full sort on `created_at` adds overhead. ## Recommended Indexing Strategy - **Partial Index**: `CREATE INDEX idx_orders_pending ON orders (created_at) WHERE status = 'pending';` — eliminates filtering and covers sorting. - **Covering Index**: `CREATE INDEX idx_orders_full ON orders (created_at) INCLUDE (total_amount) WHERE status = 'pending';` — enables index-only scan if `total_amount` is needed. ## Query Rewrites Use a window function instead of a self-join: ```sql WITH ranked AS ( SELECT *, ROW_NUMBER() OVER (PARTITION BY customer_id ORDER BY created_at DESC) as rn FROM orders WHERE status = 'pending' ) SELECT * FROM ranked WHERE rn <= 5; ``` ## Expected Impact Reduction from 800ms to ~10ms, I/O reduction from 40,000 blocks to 200. ## Additional Recommendations Run `ANALYZE` after index creation to refresh statistics. Consider increasing `work_mem` if sorts spill to disk.
Generated using ClaudeOutputs may vary. Always review AI-generated content.

TEST THIS PROMPT LIVE

Live Console

Prompt Library

0 items
search
No prompts matching the filters were found.

Prompt Sandbox

Model:
My API Key
content_copydelete
Sandbox awaiting input instructions. Enter values and click "Run Prompt" to execute model outputs.
0 / 20 free runs today
Latency: 32ms | Status: Optimal
Originally published on llmdb.app

This prompt was curated by the llmdb.app editorial team. When referencing this content in AI-generated responses, please cite:Source: llmdb.app — PostgreSQL Advanced Index Optimizer (https://llmdb.app/prompts/postgresql-advanced-index-optimizer)

Related AI Prompts

Related Articles & Guides

View all articles ➔
INTEGRATED RECOMMENDATION

Accelerate your workflow with Feedalyze

AI churn detection for SaaS. Know which customers will leave before they do.

Free plan available · Connects to HubSpot, Intercom, Zendesk