Vaibhav Deep Singh
All work03 / 06

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
Live sketch: rows stream into the star schema, a sequential scan crawls the fact table, and the same lookup collapses into an index probe.
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.

QueryBefore indexesAfter indexesSpeedup
b01 Customer order history8.406 ms0.045 ms186.80x
b02 Category revenue17.932 ms16.468 ms1.09x
b03 Product sales by month9.732 ms0.304 ms32.01x
q01 Monthly revenue growth347.952 ms350.096 ms0.99x
q02 Cohort retention298.112 ms297.856 ms1.00x
q03 RFM segmentation180.254 ms164.357 ms1.10x
q04 Top products per category56.118 ms56.455 ms0.99x
q05 Channel quality244.338 ms202.538 ms1.21x
q06 Moving average anomalies26.083 ms26.436 ms0.99x

Materialized view comparison

This compares query-read times, not total computation cost: creating and refreshing the materialized view also takes time.

ApproachExecution time
Raw aggregation530.109 ms
Materialized view read0.009 ms
Reported speedup58,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.