Data Engineering / case study
E-commerce Analytics Warehouse
A star-schema PostgreSQL warehouse for an online store, a set of analytical queries on top of it, and a benchmark measuring what indexes and materialized views actually do to query time.
- PostgreSQL
- Python
- SQL
- 186.8x
- query speedup, up to
- median of five runs after indexing
- 445,456
- rows in the sales fact table
- synthetic, two years of order lines
- 58,901x
- read speedup from the materialized view
- 0.009 ms read against a 530.109 ms aggregation
- 9
- analytical and benchmark queries
- cohort retention, RFM, anomalies, access patterns
All the data is synthetic: a Python script generates 60,000 customers and 445,456 order lines, with patterns built in (holiday-season spikes, customers who gradually stop buying, a few bestselling products) so the queries have something meaningful to find.
Timings come from one laptop, so they are relative benchmarks rather than universal performance figures. The fact table was created without secondary indexes first, to establish a baseline.

Schema
One fact table, fact_sales, with a row per order line, referencing three dimension tables: dim_customers, dim_products and dim_date.
Queries
Three additional b queries isolate access patterns for the benchmark: customer order history, category revenue over a date range, and product sales by month.
- q01: monthly revenue, month-over-month growth and a running total.
- q02: cohort retention, the share of each signup month's customers who order again in later calendar months.
- q03: RFM segmentation (recency, frequency, monetary value) and revenue by segment.
- q04: top three products per category by revenue, including gross profit.
- q05: acquisition-channel quality and customer value.
- q06: daily revenue anomalies measured against the previous seven calendar days.
Benchmark, 445,456 fact rows
Each query ran once as a warm-up, then five measured executions. Median execution time in milliseconds.
| Query | Before indexes | After indexes | Speedup |
|---|---|---|---|
| b01 Customer order history | 8.406 ms | 0.045 ms | 186.80x |
| b02 Category revenue | 17.932 ms | 16.468 ms | 1.09x |
| b03 Product sales by month | 9.732 ms | 0.304 ms | 32.01x |
| q01 Monthly revenue growth | 347.952 ms | 350.096 ms | 0.99x |
| q02 Cohort retention | 298.112 ms | 297.856 ms | 1.00x |
| q03 RFM segmentation | 180.254 ms | 164.357 ms | 1.10x |
| q04 Top products per category | 56.118 ms | 56.455 ms | 0.99x |
| q05 Channel quality | 244.338 ms | 202.538 ms | 1.21x |
| q06 Moving average anomalies | 26.083 ms | 26.436 ms | 0.99x |
Materialized view comparison
This compares query-read times, not total computation cost: creating and refreshing the materialized view also takes time.
| Approach | Execution time |
|---|---|
| Raw aggregation | 530.109 ms |
| Materialized view read | 0.009 ms |
| Reported speedup | 58,901x |
What the results show
- b01 improved 186.80x after indexing customer_id; b03 improved 32.01x with the composite (product_id, date_id) index.
- b02 improved only slightly. A broad date-range aggregation still processes many rows, so a sequential scan can stay competitive.
- q01, q02, q04 and q06 barely changed: the indexes could not eliminate most of the underlying work.
- Indexes are useful when they reduce the amount of data PostgreSQL has to touch, and are not automatically beneficial for every analytical query.
What the data shows
Revenue grows across the two-year period, with October spiking in both years (+56.6% in 2023 and +46.1% in 2024), reflecting the holiday pattern built into the generator. January 2024 drops 17.8% after the December peak, and early growth rates such as 186% need care because the business starts from a small customer base.