Building a data model
Eg - Ride hailing example
Identify business process
Ask clarifying questions - eg:
- does it support ride sharing, or only single ride
- should data model support future bookings
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
- asking questions, gathering business requirements to translate it into a list of tables
- strength of dimensional modelling concepts
- translate requirements into facts and dims
- identify slowly changing dims
- appropriately define relationships between tables etc
- Data model behaviour when a change comes in. Eg - turkey wants to change name to turkiye. How are those changes handled?
Questions:
- ride hailing companies - uber, ola
- food delivery - swiggy, zomato
- streaming platforms - netflix, youtube
- social media apps - instagram, facebook
Schema design types
- Star Schema
- Snowflake Schema
- Galaxy Schema
Star
- central fact table connects directly to denormalized dimension tables
- Simple queries, fewer joins, good performance
- Duplication of some dimension data
- BEST FOR DRILL DOWN REPORTS (moving summarized data to more detailed levels like year -> quarter -> month ->day)
Eg:
- FactSales
- DimCustomer
- DimProduct
- DimDate
Snowflake
- central fact table connects to dimension tables that are further normalized into related tables
- Less data duplication due to more normalization
- More joins and greater query complexity
Eg:
- FactSales
- DimProduct
- DimCategory
- DimProduct
Galaxy
- multiple fact tables share common dimension tables
- Supports analysis across multiple business processes
- More comples to design and query
Eg:
- FactSales
- FactInventory
- DimProduct
- DimDate
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:
- Transaction fact tables
- Record individual business events as they occur. 1 row = 1 transaction/event
- Eg: sales record storing product id, quantity etc
- Periodic snapshot fact tables
- records state of business at regular intervals (hour, day, week, month etc)
- Eg - daily account balances, oil tanker levels etc
- Accumulating snapshot fact tables
- Tracks a business process from beginning to completion. Row is updated as process progresses through milestones.
- One row per process instance. Store milestone dates (fk to DimDate)
- Eg - order fulfillment.
Other types of fact tables:
- Factless fact table
- Records an event or relationship without necessarily storing numerical measures. So typically only contains foreign keys to dimensions.
- Eg - Student attendence (table cols - dateId, studentId, classId)
- Aggregate fact table
- Aggregate data from more granular fact tables
- Eg - monthly total sales of each product category
- Consolidated fact table
- Combines measures from multiple business processes into a common fact table when compatible
- Eg - combine sales and returns into a common table, to compare financial impacts
3.
Resources:
Property of fact - additivity
This makes 3 types of facts
- additive
- semi-additive
- 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
- Static - never changes
- Slowly changing
- Conformed - used by multiple facts, and has the same meaning across the model. Eg - list of places.
- Degenerate - fact storing dimensional values. Eg - invoice number
- Junk - too many small dims put into one dim, even though each of those small dims are unrelated
- Role-playing - values associated with multiple fats. Eg - dim_date can take the role of purchaseDate, holidayDate etc
- 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:
- Manish Kumar playlist - https://www.youtube.com/watch?v=UiTvqSd52ak&list=PLTsNSGeIpGnGP8A74Ie1PgqHhewsqD3fv
- chatGPT
- Kimball - Dim modelling techniques