Prompt lesson · 10 prompts
SQL Query Optimization prompts for Database Administrators
10 ready-to-use prompts from our AI for Database Administrators course. Copy one, fill in the {{placeholders}}, and paste it into ChatGPT, Claude, Gemini or any other AI.
Identify Slow-Performing Queries
Use this when you need to analyze SQL code or execution plans to pinpoint performance bottlenecks and recommend optimizations.
Role You are a senior database performance engineer who diagnoses slow queries and provides precise, actionable optimization strategies.
Context you provide
- {{sql_code}} – the SQL queries or code to analyze
- {{execution_plans}} – optional: execution plans for deeper analysis
- {{database_type}} – the database system (e.g., PostgreSQL, MySQL, SQL Server)
- {{performance_goals}} – what you want to improve (e.g., response time, resource usage)
- {{schema_info}} – optional: relevant table structures or indexes
Instructions
- Ask for missing inputs before starting.
- Analyze the provided SQL code and/or execution plans to identify the top three slow-performing queries.
- For each query, explain why it is underperforming (e.g., full table scans, missing indexes, inefficient joins).
- Recommend specific optimizations, such as index tuning, query rewriting, or schema restructuring.
- If execution plans are provided, identify bottlenecks like high-cost operations or loops.
- Suggest database configuration adjustments if relevant.
Output format
- A structured report with sections: Top Slow Queries, Root Cause Analysis, Optimization Recommendations, and Expected Impact.
- Use technical but clear language; include code snippets for suggested rewrites.
- Length: 400-600 words.
Guardrails
- Do not assume database specifics not provided; ask for clarification.
- Avoid suggesting risky changes without noting potential trade-offs.
- Stay within the scope of query performance; do not redesign the entire database.
Example SQL code: SELECT * FROM orders WHERE customer_id IN (SELECT customer_id FROM customers WHERE signup_date > '2024-01-01'); execution plans: [paste plan], database: PostgreSQL, performance goals: reduce query time from 5s to <1s.
Open this prompt Analysis · Advanced
Optimize Database Indexes
Use this when you need to improve database query performance by analyzing and adjusting indexes.
Role You are a database performance expert specializing in index optimization. Your goal is to analyze schemas and query patterns to recommend effective index strategies that enhance query performance.
Context you provide
- {{schema}}: The database schema you want analyzed, including tables, columns, and existing indexes.
- {{query_patterns}}: (Optional) The typical queries or workload patterns to consider.
- {{goals}}: (Optional) Specific performance goals or constraints.
Instructions
- If {{schema}} is not provided, ask for it before proceeding.
- Analyze the schema to identify high-impact tables and columns for indexing.
- Recommend new indexes, modifications to existing ones, and removal of redundant or unused indexes.
- Prioritize recommendations based on potential performance impact and ease of implementation.
- Explain how each recommendation improves query performance.
Output format Provide a structured report with sections: 'Recommended New Indexes', 'Modifications', 'Removals', and 'Prioritized Action List'. Use tables where helpful. Keep explanations concise and technical.
Guardrails
- Do not invent schema details; base recommendations solely on provided information.
- Flag any assumptions about query patterns or data distribution.
- Stay within the scope of index optimization; do not suggest unrelated schema changes.
Example {{schema}} = 'users(id, email, created_at), orders(id, user_id, status, created_at)', {{query_patterns}} = 'frequent queries filtering by user_id and status'.
Open this prompt Analysis · Advanced
Rewrite Queries for Efficiency
Use this when you need to improve the performance of complex SQL queries by rewriting them more efficiently.
Open this prompt Writing · Intermediate
Optimize SQL Join Performance
Use this when you need to improve the performance of SQL queries by optimizing join types, order, or schema design.
Open this prompt Analysis · Advanced
Optimize Subquery Performance
Use this when you need to improve the performance of SQL queries that use subqueries.
Open this prompt Writing · Intermediate
Implement Query Caching
Use this when you want to reduce database load by caching frequently executed queries.
Open this prompt Planning · Intermediate
Parameterize Database Queries
Use this when you need to parameterize queries to improve performance, avoid recompilations, and enhance security.
Open this prompt Learning · Intermediate
Query Optimizer Statistics Management
Use this when you need a practical plan for keeping database query optimizer statistics accurate and up to date.
Open this prompt Planning · Advanced
Optimize Parallel Query Execution
Use this when you need to design or improve parallel query execution to make database processing faster on multi-core systems.
Open this prompt Planning · Intermediate
Balance Query Workloads
Use this when you need to distribute database query loads across multiple servers for better performance and reliability.
Open this prompt Planning · Advanced