advancedTop 30 Scenario-Based Questions

You have a running total query that's very slow on 10M rows. How do you optimize it?

Running total: SUM(amount) OVER (ORDER BY date ROWS BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW). For 10M rows: 1) Index on the ORDER BY column: CREATE INDEX ON orders(order_date, amount) — covering index avoids heap. 2) Partition the table by date — queries on recent data only process recent partition. 3) Incremental materialized view: instead of computing running total on query, maintain a cumul

This is a Pro chapter

Sign in, then upgrade to Pro or Power to unlock this and the full Databases Mastery library.

You have a running total query that's very slow on 10M rows. How do you optimize it?

Next Step

Continue to How would you implement a job queue in PostgreSQL supporting multiple workers without losing or duplicating jobs?← Back to all SQL questions