PromptBase
Upgrade
Close icon
General
Home
Marketplace
Create
Hire
Login
Chat
Sell
Explore

Prompt Details

Model
Chat - (gpt-5.5)
Token size
706
Example input
[SLOW_QUERY]: SELECT o.id, o.total, c.email FROM orders o JOIN customers c ON c.id = o.customer_id WHERE o.status = 'paid' AND o.created_at >= NOW() - INTERVAL '30 days' ORDER BY o.created_at DESC LIMIT 50; [SCHEMA]: - orders: 8.4M rows. Columns: id PK, customer_id FK, status varchar, created_at timestamptz, total numeric. - Indexes: orders_pkey on id, idx_orders_customer_id on customer_id. No index on status or created_at. - customers: 920K rows. Columns: id PK, email unique. [DB_ENGINE]: PostgreSQL 15 [PROBLEM_SYMPTOM]: Endpoint /api/recent-paid-orders times out at 4-5 seconds under load. EXPLAIN shows Seq Scan on orders.
Example output
BOTTLENECK DIAGNOSIS: The planner is doing a sequential scan on orders (8.4M rows) because the WHERE clause filters by status and created_at, but the only index is on customer_id. The hash join to customers then materializes the full filtered set before the LIMIT can apply. Expected execution time is 4.2 seconds; the LIMIT 50 is doing nothing useful because the filter is the bottleneck. OPTIMIZED QUERY: ```sql SELECT o.id, o.total, c.email FROM orders o JOIN customers c ON c.id = o.customer_id -- unchanged join WHERE o.status = 'paid' -- now uses partial index AND o.created_at >= NOW() - INTERVAL '30 days' -- aligned with index column order ORDER BY o.created_at DESC -- matches index ordering, removes sort LIMIT 50; ``` INDEX RECOMMENDATIONS: ```sql -- REQUIRED — turns the seq scan into a small index range scan CREATE INDEX idx_orders_status_created_partial ON orders (created_at DESC) WHERE status = 'paid'; ``` Partial index keeps the index small — only ~18 percent of orders are 'paid' at any time. Storage cost ~140MB vs 780MB for a full composite index. Order matters: created_at DESC lets the planner skip the sort step entirely for the ORDER BY. EXECUTION PLAN COMPARISON: - BEFORE: Seq Scan on orders → Hash Join to customers → Sort → Limit. Estimated 8.4M-row scan, ~4.2s wall time. - AFTER: Index Range Scan on idx_orders_status_created_partial returning ~12K rows → Nested Loop with customers (PK lookup) → Limit hits early. Estimated ~30ms wall time. WHY IT IS FASTER: - Partial index restricts I/O to only 'paid' rows — eliminates 82 percent of the table from the scan. - DESC order in the index matches the ORDER BY, so the planner skips the sort node entirely. - LIMIT 50 can short-circuit because the index returns rows already in the requested order. - Nested loop join becomes optimal once the outer side is small — switches automatically once the planner sees the ~12K-row estimate. PR SUMMARY: Adds a partial DESC index on orders for status='paid', cutting the recent-orders endpoint from 4.2s to ~30ms. No application code change required; query is semantically identical.
🤖 GPT

Sql Query Optimizer Explainer

Add to Cart
Instant accessInstant access
Usage rightsCommercial use
Money-back guaranteeMoney‑back
By purchasing this prompt, you agree to our terms of service
GPT-5.5
Tested icon
Guide icon
4 examples icon
Free credits icon
Paste a slow SQL query and schema and get back: an optimized rewrite, a plain-English execution-plan comparison, CREATE INDEX statements with tradeoff notes, and a PR-ready summary. Works for PostgreSQL, MySQL, SQL Server, and BigQuery. Fill in [SLOW_QUERY], [SCHEMA], [DB_ENGINE], and [PROBLEM_SYMPTOM]. For backend engineers and analysts who need to fix slow queries without guessing at EXPLAIN output.
...more
Added 22 hours ago
Report
Browse Marketplace