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

Prompt Details

Model
(r1)
Token size
232
Example input
[SQL Query]: SELECT o.*, c.name, c.email FROM orders o, customers c WHERE o.customer_id = c.id AND o.created_at >= '2026-01-01' AND o.created_at < '2026-02-01' ORDER BY o.created_at DESC; [Table Schema and Indexes]: orders (id PK, customer_id, created_at, status, total_cents, plus 17 more columns); customers (id PK, name, email, created_at). Existing indexes: primary keys only. No index on orders.created_at or orders.customer_id. [Database Engine]: PostgreSQL 15 [Approximate Row Count]: orders ~8 million rows, customers ~300,000 rows
Example output
Optimized query: SELECT o.id, o.customer_id, o.created_at, o.status, o.total_cents, c.name, c.email FROM orders o JOIN customers c ON c.id = o.customer_id WHERE o.created_at >= '2026-01-01' AND o.created_at < '2026-02-01' ORDER BY o.created_at DESC; What changed and why: 1. Implicit comma-join replaced with explicit JOIN ... ON — same result, but makes the join condition unambiguous to the planner and to future readers; an implicit join still requires Postgres to infer it's an inner join from the WHERE clause, which is more fragile if that clause is edited later. 2. SELECT o.* replaced with an explicit column list — orders has 22 columns; pulling all of them when only 5 are used adds unnecessary I/O and prevents an index-only scan on the new index below. 3. Date range on created_at kept as a half-open interval (>= / <) rather than BETWEEN — avoids ambiguity around inclusive end-of-day boundaries and lets the planner use a straightforward range scan. Suggested index: CREATE INDEX idx_orders_created_at_covering ON orders (created_at DESC) INCLUDE (customer_id, status, total_cents); This serves both the range filter and the ORDER BY o.created_at DESC in one pass (no separate sort step), and the INCLUDEd columns let Postgres satisfy the query from the index alone for those fields, cutting heap fetches on an 8M-row table. Trade-offs: this index adds roughly 15-20% write overhead to orders inserts/updates on created_at-heavy write paths — worth it here since orders are append-mostly and this looks like a recurring reporting query, but flag it to your team if orders also sees frequent updates to created_at itself. If customers grows past a few million rows, the existing primary key index on customers.id is still enough for this join — no change needed there.
🐋 DeepSeek

Sql Query Optimizer And Explainer

Add to Cart
PromptBase Select
Or get it with Select10 Select prompts/mo — under $2 each
Learn more
Instant accessInstant access
Usage rightsCommercial use
Money-back guaranteeMoney‑back
By purchasing this prompt, you agree to our terms of service
R1
Tested icon
Guide icon
4 examples icon
Free credits icon
Paste a slow SQL query plus your table schema and it returns an optimized rewrite, a numbered breakdown of what changed and why, a suggested index with reasoning, and the trade-offs of the fix. Asks for missing details like row count or DB engine instead of guessing performance characteristics. Built for engineers who need query performance explained, not just fixed.
...more
Added 14 hours ago
Report
Browse Marketplace