Part 4 of 10
Shared links between records
- Ingredients and steps are one-to-many (one-to-few).
- Cuisine is many-to-one: many recipes share one cuisine, and a recipe has one cuisine.
- If publishers become records referred to by ID, recipes and publishers are many-to-many: a recipe can appear in cookbooks from several publishers, and a publisher has many recipes.
Relational databases model many-to-many with an associative table (join table): each row links one recipe ID to one publisher ID.
These links do not fit inside one self-contained document, so they push you toward normalizing. In a document model, you group what belongs together and point to other documents (publishers, cuisines) by ID.
Many-to-many links often need queries both ways: publishers of a recipe, and recipes of a publisher.
- Option 1: store the link on both sides, which is denormalized, so the sides can disagree.
- Option 2: store it once and use secondary indexes (chapter 4) to search both ways, as in indexing both columns of a SQL join table.
For option 2, a document database must index a field inside the objects of a nested array. Many document stores, and relational databases that handle JSON, can do this.
Warehouse schemas: stars and snowflakes
Warehouses are usually relational, with conventions such as star schemas, snowflake schemas, dimensional modeling, and one big table. They serve business analysts, and ETL jobs copy operational data into them.
Take a chain of cinemas. In a star schema, the center is a fact table, say fact_tickets. Each row is one event at one moment: a customer buying a ticket. On a website, a fact row might be one page view or click. Facts stay as single events for flexible analysis, so fact tables get enormous, often petabytes.
Each fact column is either:
- An attribute, like ticket price and the fee paid to the film distributor, so profit can be worked out.
- A foreign key to a dimension table.
Dimensions describe each event: which customer, film, cinema, and day, how it was paid, and which promotion led to it. A dim_film row holds a film's title, genre, length, and studio, and each fact row says which film was watched. Queries often join the fact table with several dimensions. Dates are often a dimension too, so you can tag school holidays and filter by them. The name comes from the picture: the fact table sits in the middle, with dimension tables spreading out like rays.
A snowflake schema breaks dimensions into subdimensions, like dim_film pointing to separate studio and genre tables. It is more normalized, but analysts often prefer the simpler star.
Warehouse tables are wide. Fact tables often have over 100 columns, sometimes several hundred. Dimension tables keep every detail that might matter; a dim_cinema row might note screen count, café, parking, opening date, last refit, and distance from the nearest station.
These schemas are mostly many-to-one: fact to dimension, dimension to subdimension. Other relationships are usually denormalized: three tickets in one order are just three fact rows with the same customer, cinema, and time.
One big table (OBT) drops the dimension tables and folds their data into the fact table, like running the join ahead of time. It takes more storage and sometimes speeds up queries. Copying is fine here because analytics data is a history that rarely changes, apart from fixing errors, so OLTP worries about consistency and write cost matter less.