data modelling

4m read · 767 words

Building a data model

Eg - Ride hailing example

Identify business process
Ask clarifying questions - eg:

Identify events and entities associated. Helps to figure out fact and dim tables.

List down arrtibutes of table
Eg - driverid, arrived at, pickup at etc

interviewer is looking for

Questions:

Schema design types

  1. Star Schema
  2. Snowflake Schema
  3. Galaxy Schema

Star

Eg:

Snowflake

Eg:

Galaxy

Eg:

Rule of thumb: Use a star schema for most BI dashboards and drill-down reports. The critical factor is designing dimensions with the correct hierarchies; drill-down capability itself does not require a particular schema.

Fact tables types

3 major types of fact tables:

  1. Transaction fact tables
    1. Record individual business events as they occur. 1 row = 1 transaction/event
    2. Eg: sales record storing product id, quantity etc
  2. Periodic snapshot fact tables
    1. records state of business at regular intervals (hour, day, week, month etc)
    2. Eg - daily account balances, oil tanker levels etc
  3. Accumulating snapshot fact tables
    1. Tracks a business process from beginning to completion. Row is updated as process progresses through milestones.
    2. One row per process instance. Store milestone dates (fk to DimDate)
    3. Eg - order fulfillment.

Other types of fact tables:

  1. Factless fact table
    1. Records an event or relationship without necessarily storing numerical measures. So typically only contains foreign keys to dimensions.
    2. Eg - Student attendence (table cols - dateId, studentId, classId)
  2. Aggregate fact table
    1. Aggregate data from more granular fact tables
    2. Eg - monthly total sales of each product category
  3. Consolidated fact table
    1. Combines measures from multiple business processes into a common fact table when compatible
    2. Eg - combine sales and returns into a common table, to compare financial impacts

    3. 3.

Resources:

Property of fact - additivity

This makes 3 types of facts

  1. additive
  2. semi-additive
  3. non-additive

additive - values can be added across all relevant dimensions without losing its meaning

eg - sales amount

semi-additive

The fact can be summed across some dimensions, but not across others

Eg - account balance. We can add all account balances on one particular date. But we cannot add account balance across all dates.

Other examples - account balance, inventory on hand, headcount, outstanding loan balance

Non-additive

cannot be summed across dimensions.

Eg - profit margin. Total profit margin isn't simply a sum-total of all stored profit margins. It has to be re-calculated by adding all profit and all revenue, and then finding their ratio.

Types of dims

  1. Static - never changes
  2. Slowly changing
  3. Conformed - used by multiple facts, and has the same meaning across the model. Eg - list of places.
  4. Degenerate - fact storing dimensional values. Eg - invoice number
  5. Junk - too many small dims put into one dim, even though each of those small dims are unrelated
  6. Role-playing - values associated with multiple fats. Eg - dim_date can take the role of purchaseDate, holidayDate etc
  7. Shrunken - dim that's further divided. Eg - dimproduct is further snowflaked into dimproduct_category
SCD type Main purpose Typical additional columns
Type 0 Preserve original value None
Type 1 Overwrite changes None required
Type 2 Preserve complete history surrogate key, validfrom, validto, iscurrent, versionnumber(optional)
Type 3 Preserve limited history Previous-value columns (eg - previous_city)
Type 4 Separate historical records Current snapshot in one table, and previous history (scd2) in another table
Type 6 Combine SCD 1, 2, 3 SCD2 columns plus current/previous values

Misc

Bridge tables for m:n relationships

Resources:

Colophon 767 words · 4m read
Written as a markdown note in Obsidian. Built into this page by a Python script on 2026-10-24.

Pages