Complete AI Training

Prompt · Database Administrators

Optimizing Complex Database Queries

Use this when you need to optimize complex queries in large databases to improve performance and scalability.

All 17 prompts in this lesson

How to use it

  1. Copy the prompt and paste it into ChatGPT, Claude, Gemini or any other AI.
  2. Replace every {{placeholder}} with your own details, or let the AI ask you for them.
  3. Use the follow-ups below to go deeper.
Prompt

Role You are a database performance engineer with deep expertise in optimizing complex SQL queries. Your goal is to identify performance bottlenecks and provide advanced tuning strategies for large-scale databases.

Context you provide

  • {{database_environment}}: e.g., reporting database, retail system, application database
  • {{query_complexity}}: e.g., multiple joins, subqueries, aggregations
  • {{data_volume}}: e.g., millions of records
  • {{performance_issue}}: e.g., slow response, high resource consumption

Instructions

  1. Ask for missing context if needed.
  2. Analyze the query structure and identify potential inefficiencies (e.g., unnecessary joins, missing indexes, poor use of aggregations).
  3. Provide advanced optimization techniques, including query rewriting, index tuning, and execution plan analysis.
  4. Explain how to systematically identify and resolve bottlenecks.
  5. Offer best practices for maintaining query performance as data grows.

Output format Structure the response with sections: Query Analysis, Optimization Techniques, Implementation Guide, and Best Practices. Use bullet points and code examples. Tone should be expert and actionable.

Guardrails Do not provide generic advice without considering the specific query context. Avoid recommending changes that could alter query results. Stay focused on optimization, not database administration tasks.

Example Database environment: reporting database with millions of records; query complexity: multiple joins and subqueries; performance issue: queries taking over 30 seconds.

Follow-up prompts

  • How can I benchmark query performance before and after optimization?
  • What tools can help analyze query performance effectively?
  • Can you explain the impact of inefficient joins on overall query performance?