What is your approach to optimizing slow SQL queries?
Using the provided PostgreSQL schema, produce an equivalent query for the required result. Explain how you would inspect the execution plan, reduce unnecessary work, and select appropriate indexes without changing the result.
customer_name, order_count, and total_spent.total_spent descending, then customer_id ascending for ties.| Column | Type | Description |
|---|---|---|
| customer_idPK | INT | Unique customer identifier |
| customer_name | VARCHAR(100) | Customer display name |
| VARCHAR(255) | Customer email address |
| Column | Type | Description |
|---|---|---|
| order_idPK | INT | Unique order identifier |
| customer_id | INT | References customers.customer_id |
| order_date | DATE | Date on which the order was placed |
| status | VARCHAR(20) | Order processing status |
| Column | Type | Description |
|---|---|---|
| order_item_idPK | INT | Unique order line identifier |
| order_id | INT | References orders.order_id |
| product_name | VARCHAR(100) | Product description |
| quantity | INT | Number of units in the order line |
| unit_price | DECIMAL(10,2) | Price per unit |