From 3 Hours to 40 Minutes: The SQL Prompt Toolkit That Saved Our Analytics
We've all been there: staring at a query that's been running for 15 minutes, knowing it shouldn't take more than 30 seconds. Or writing yet another ad-hoc report that's 95% similar to last week's, but with a different date range. Over time, this eats hours of every workday. When our team decided to tackle this, we didn't just optimize individual queries — we built a toolkit of prompts for AI assistants that now handles the heavy lifting. The result: a 70% reduction in the time we spend writing and debugging SQL.
AI language models have become remarkably good at understanding database schemas and generating efficient SQL. They can explain execution plans, suggest index improvements, and even rewrite queries for better performance. But the key is knowing how to prompt them effectively. This isn't about magic — it's about giving the AI the right context, constraints, and examples. Here's the exact toolkit we use, with prompts you can copy and adapt.
1. The All-Purpose Query Optimizer
Task: When you have a slow query and need immediate suggestions for optimization.
Prompt:
You are a senior PostgreSQL performance expert. Analyze the following query and schema, then identify performance bottlenecks. Provide specific, actionable recommendations in order of impact. For each recommendation, show the rewritten query or DDL change, and estimate the expected performance gain.
Schema:
```sql
CREATE TABLE orders (
id BIGSERIAL PRIMARY KEY,
customer_id INT NOT NULL,
order_date TIMESTAMPTZ NOT NULL DEFAULT now(),
status TEXT NOT NULL,
total NUMERIC(10,2)
);
CREATE INDEX idx_orders_customer_date ON orders(customer_id, order_date DESC);
Query:
SELECT customer_id, COUNT(*) AS order_count, SUM(total) AS total_spent
FROM orders
WHERE order_date >= '2026-01-01'
GROUP BY customer_id
HAVING COUNT(*) > 10
ORDER BY total_spent DESC;
Please provide a detailed analysis.
...and that's precisely where the real work begins. Once the initial burst of motivation fades, the daily grind of showing up, making incremental progress, and staying consistent is what separates those who merely start from those who actually finish.
The key is to build systems rather than rely on willpower. Willpower is a finite resource that depletes throughout the day, but a well-designed routine becomes automatic. When you remove the decision-making friction—when you don't have to think about whether you'll do the work, only when—you're far more likely to sustain momentum over the long haul.
This also means accepting that some days will be mediocre. Not every session will be inspired, and that's fine. The point isn't perfection; it's showing up enough times that the average of your efforts trends upward. A so-so day still moves the needle when stacked against weeks of inaction.
Another crucial element is tracking your progress visibly. Whether it's a simple checklist, a habit tracker, or a journal, seeing the chain of completed days creates a powerful psychological incentive not to break it. The streak becomes its own reward, a small but meaningful source of accountability that keeps you anchored.
And when you stumble—because you will—treat it as data, not as a verdict. Ask yourself what got in the way, adjust your approach, and get back on track the next day. Missing one workout, one writing session, or one study block is a hiccup; missing two weeks is a pattern. The fastest way back is always to resume, not to wait for a
Comments