How do you optimize a slow-running SQL query when dealing with millions of rows of transaction data?
Use the provided PostgreSQL tables to produce a representative optimized result for posted transactions in the first quarter of 2025. The query should calculate monthly account totals, prior-month totals, and each account's monthly rank.
month, account_id, account_name, monthly_amount, previous_month_amount, monthly_rank.month, monthly_rank, and account_id.| Column | Type | Description |
|---|---|---|
| transaction_idPK | INT | Unique transaction identifier |
| account_id | INT | Account associated with the transaction |
| category_id | INT | Transaction category identifier |
| transaction_date | TIMESTAMP | Timestamp when the transaction occurred |
| status | VARCHAR(20) | Transaction processing status |
| amount | NUMERIC(14,2) | Transaction amount |
| Column | Type | Description |
|---|---|---|
| account_idPK | INT | Unique account identifier |
| account_name | VARCHAR(120) | Account display name |
| account_type | VARCHAR(40) | Account classification |
| Column | Type | Description |
|---|---|---|
| category_idPK | INT | Unique category identifier |
| category_name | VARCHAR(80) | Transaction category name |
| category_group | VARCHAR(60) | Higher-level category grouping |