Use it when you want to begin query optimization work without writing the first draft from scratch.
Slow Query Analysis AI Prompt
Identify and fix slow queries in this database. Database: {{database}} Monitoring tool: {{tool}} (pg_stat_statements, slow query log, pgBadger, DataDog) Problem symptoms: {{symp... Copy this prompt template, run it in your AI tool, and use related prompts to continue the workflow.
Identify and fix slow queries in this database.
Database: {{database}}
Monitoring tool: {{tool}} (pg_stat_statements, slow query log, pgBadger, DataDog)
Problem symptoms: {{symptoms}}
1. Find slowest queries with pg_stat_statements:
SELECT query,
calls,
mean_exec_time,
total_exec_time,
stddev_exec_time,
rows / calls AS avg_rows
FROM pg_stat_statements
ORDER BY total_exec_time DESC
LIMIT 20;
Focus on: highest total_exec_time (biggest impact on the system overall), not just highest mean.
2. Find queries with high variance (stddev >> mean):
-- These queries are sometimes fast, sometimes very slow (plan instability)
SELECT query, mean_exec_time, stddev_exec_time,
stddev_exec_time / NULLIF(mean_exec_time, 0) AS cv
FROM pg_stat_statements
WHERE calls > 100
ORDER BY cv DESC;
3. Slow query log:
log_min_duration_statement = 1000 -- log all queries > 1 second
pgBadger: parse PostgreSQL logs into an HTML report with top slow queries, lock waits, and error counts
4. Common slow query patterns:
N+1 queries: app issues 1 query to get N records, then N queries for details
Fix: rewrite as a single JOIN query
Missing index on WHERE / JOIN column:
Fix: EXPLAIN ANALYZE the query; add index on the Seq Scan column
Returning too many rows:
Fix: add LIMIT; use pagination (keyset pagination is faster than OFFSET for large pages)
Implicit type cast prevents index use:
WHERE user_id = '12345' -- user_id is INTEGER; string causes type cast → no index
Fix: match parameter type to column type
Large IN (...) clause:
WHERE id IN (1,2,3,...,10000) -- creates a large OR condition
Fix: use a temporary table or VALUES() with JOIN instead
5. Auto_explain for plan logging:
LOAD 'auto_explain';
SET auto_explain.log_min_duration = 1000;
SET auto_explain.log_analyze = true;
-- Logs the execution plan for every query > 1 second
Return: slow query identification queries, pattern diagnosis for each slow query, fix recommendations, and auto_explain configuration.When to use this prompt
Use it when you want a more consistent structure for AI output across projects or datasets.
Use it when you want prompt-driven work to turn into a reusable notebook or repeatable workflow later.
Use it when you want a clear next step into adjacent prompts in Query Optimization or the wider Database Engineer library.
What the AI should return
The AI should return a structured result that covers the main requested outputs, such as Find slowest queries with pg_stat_statements:, Find queries with high variance (stddev >> mean):, Slow query log:. The final answer should stay clear, actionable, and easy to review inside a query optimization workflow for database engineer work.
How to use this prompt
Open your data context
Load your dataset, notebook, or working environment so the AI can operate on the actual project context.
Copy the prompt text
Use the copy button above and paste the prompt into the AI assistant or prompt input area.
Review the output critically
Check whether the result matches your data, assumptions, and desired format before moving on.
Chain into the next prompt
Once you have the first result, continue deeper with related prompts in Query Optimization.
Frequently asked questions
What does the Slow Query Analysis prompt do?+
It gives you a structured query optimization starting point for database engineer work and helps you move faster without starting from a blank page.
Who is this prompt for?+
It is designed for database engineer workflows and marked as intermediate, so it works well as a guided starting point for that level of experience.
What type of prompt is this?+
Slow Query Analysis is a single prompt. You can copy it as-is, adapt it, or use it as one step inside a larger workflow.
Can I use this outside MLJAR Studio?+
Yes. The prompt text works in other AI tools too, but MLJAR Studio is the best fit when you want local execution, visible Python code, and reusable notebooks.
What should I open next?+
Natural next steps from here are Deadlock and Lock Analysis, Query Execution Plan Analysis.