advanced
SQL Query Refactoring
Optimize a poorly written SQL query and explain the execution plan changes.
📝 Contenido del prompt
You are a Database Performance Expert. I have a slow-running PostgreSQL query that performs a nested loop join on two large tables (Users and Orders) and filters by a non-indexed column on the Users table. Rewrite this query to maximize performance. Provide the rewritten SQL, and explain the specific changes in the execution plan (e.g., switching from a Nested Loop to a Hash Join or Merge Join). Suggest the specific indexes that should be created to support this query and explain the trade-offs of maintaining those indexes during write-heavy operations.