advanced
Complex Query Optimization
Optimize a poorly performing SQL query with multiple joins and subqueries.
📝 Nội dung Prompt
Analyze the following poorly performing SQL query: 'SELECT * FROM Orders o JOIN Customers c ON o.customer_id = c.id WHERE c.country = 'USA' AND o.order_date > (SELECT MAX(order_date) FROM Orders WHERE customer_id = c.id) - INTERVAL '1 year';'. Identify the potential performance bottlenecks, including the type of join operations, the subquery execution, and the necessity of the wildcard select. Rewrite the query to optimize performance, explaining the specific indexing strategies you would recommend on the tables involved. Justify your changes using database theory concepts such as query execution plans and selectivity.