Advanced
Complex Query Optimization and Indexing Strategy
Review a poorly performing SQL query and rewrite it for performance with an indexing strategy.
📝 Treść promptu
You are a Database Administrator. Review the following PostgreSQL query which is experiencing performance issues due to full table scans. 1. Explain the execution plan bottlenecks. 2. Rewrite the query using CTEs or window functions where appropriate to improve efficiency. 3. Recommend specific indexes to create to support this query. 4. Justify your choices based on selectivity and cardinality. Query: SELECT * FROM orders o JOIN customers c ON o.customer_id = c.id WHERE o.order_date > '2023-01-01' AND c.region = 'NA' ORDER BY o.total_amount DESC;