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 without upgrading.
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
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:\\\\\\\\n\\\\\\\\n**Database Context:**\\\\\\\\n- Main tables: [LIST WITH RECORD COUNTS]\\\\\\\\n- Write volume: [NUMBER] inserts/updates per hour\\\\\\\\n- Read volume: [NUMBER] queries per second\\\\\\\\n- Total size: [VALUE] GB\\\\\\\\n- Hosting: [RDS/SUPABASE/SELF-HOSTED] with [RAM] memory\\\\\\\\n\\\\\\\\n**Problematic Queries (paste SQL or describe):**\\\\\\\\n1) [QUERY 1 — e.g., product search with multiple filters]\\\\\\\\n2) [QUERY 2 — e.g., aggregate report with JOINs]\\\\\\\\n3) [QUERY 3 — e.g., real-time dashboard with counts]\\\\\\\\n\\\\\\\\n**1) EXPLAIN ANALYZE Analysis:**\\\\\\\\n- For each query, show how to interpret the execution plan\\\\\\\\n- Identify: unnecessary Seq Scans, expensive Nested Loops, inefficient Hash Joins\\\\\\\\n- Estimated cost vs. actual cost\\\\\\\\n- Estimated rows vs. actual rows (variance = outdated statistics)\\\\\\\\n\\\\\\\\n**2) Indexing Strategy:**\\\\\\\\n- B-tree indexes for equality and range filters\\\\\\\\n- GIN indexes for full-text search and arrays/JSONB\\\\\\\\n- Partial indexes (WHERE condition) for frequent queries\\\\\\\\n- Composite indexes (column order matters)\\\\\\\\n- Covering indexes (INCLUDE) to avoid table lookups\\\\\\\\n- Estimated size of each index\\\\\\\\n- Trade-off: write impact vs. read gain\\\\\\\\n\\\\\\\\n**3) Query Refactoring:**\\\\\\\\n- CTEs vs. subqueries (materialization in PG 14+)\\\\\\\\n- Using LATERAL JOIN for correlated queries\\\\\\\\n- Materialized Views for heavy reports (refresh strategy)\\\\\\\\n- Table partitioning for large tables by date\\\\\\\\n- Connection pooling with PgBouncer\\\\\\\\n\\\\\\\\n**4) PostgreSQL Configuration:**\\\\\\\\n- shared_buffers, work_mem, effective_cache_size\\\\\\\\n- max_connections vs. connection pooling\\\\\\\\n- Autovacuum tuning for tables with heavy UPDATE/DELETE\\\\\\\\n- pg_stat_statements for continuous monitoring\\\\\\\\n\\\\\\\\nFor 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
- 1Replace the key placeholders first: VERSION: 14/15/16, PROJECT NAME, LIST WITH RECORD COUNTS, NUMBER.
- 2Replace any bracketed placeholders like [this] with your own context.
- 3Add extra background information when you want more tailored results.
- 4Combine multiple prompts in one conversation when you need a richer output.
- 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 allREST API design with versioning and OpenAPI documentation
Design a robust REST API with naming conventions, versioning, pagination, and Swagger documentation.
Best for
Create a professional, well-documented REST API that follows industry best practices and facilitates integration by frontend teams and external partners.
Complete Authentication System with JWT, Refresh Tokens, and RBAC
Secure authentication and authorization implementation with rotating tokens and role-based access control.
Best for
Build a robust authentication and authorization system that protects the API against common attacks and implements granular permission control.
Monolith to Microservices Migration with Event-Driven Architecture
Decomposition strategy for a Node.js monolith into microservices with asynchronous message-based communication.
Best for
Plan and execute a gradual migration from monolith to microservices without service interruption, using asynchronous communication patterns with message queues.
Multi-Layer Caching Strategy with Redis and CDN
Multi-layer cache implementation to reduce latency and infrastructure costs.
Best for
Design a caching strategy that reduces database load by 80%+ and improves API response time to under 50ms on the most accessed endpoints.
Explore other prompt categories
Move sideways into adjacent libraries when the current category is not the full answer.
Free browsing stays open. Premium prompts unlock the reusable workflow layer.
Use the guides and role paths to validate the job first. Upgrade when you want the full prompt text, editable premium prompts, and the surrounding course paths in one place.
Free access
- Browse guides, role paths, and category pages.
- Preview prompts before you decide to upgrade.
- Find the right starting point without friction.
Membership access
- Unlock premium prompts and the full copy text.
- See more workflow paths and course connections.
- Keep the reusable templates in one place.