Advanced
Complex Query Optimization
Optimize a slow, complex SQL query involving multiple joins and subqueries.
📝 Prompt İçeriği
Given a PostgreSQL database schema with tables for users, orders, order_items, products, and reviews, optimize the following query: 'SELECT u.name, COUNT(o.id) as total_orders, SUM(oi.quantity * p.price) as total_spent FROM users u LEFT JOIN orders o ON u.id = o.user_id LEFT JOIN order_items oi ON o.id = oi.order_id LEFT JOIN products p ON oi.product_id = p.id WHERE o.created_at > '2023-01-01' GROUP BY u.name HAVING SUM(oi.quantity * p.price) > 1000'. Explain the execution plan bottlenecks, suggest appropriate indexing strategies, and rewrite the query using CTEs (Common Table Expressions) or window functions to improve performance by an estimated factor of 10x.