सामग्री पर जाएँ

लेसन 17 · Loading Data

Loading Fact Tables

Load fact tables correctly: look up surrogate keys, handle late-arriving dimensions and facts, rerun a day safely and understand how MERGE fits in.

Dimensions are loaded first so that facts can point to them. A fact load then swaps each source identifier for a surrogate key, applies the grain and writes the rows. Most real difficulty comes from data that is late or incomplete.

The key lookup pipeline

For each incoming fact row:

  1. Convert dates to date keys, using the agreed time zone.
  2. Find each dimension row by natural key. For Type 2 dimensions, also match the event date to the validity period.
  3. Replace missing matches with a special key or placeholder row.
  4. Insert the row at the declared grain, without creating duplicates.

Use LEFT JOIN in lookups. With an inner join, any row with a missing match silently disappears.

Late-arriving dimensions

Sometimes a fact arrives before its dimension record: a new product sells before the product system's nightly export. This is also called an early-arriving fact.

You could hold the fact back, point it to the generic Unknown row, or create an inferred member: a placeholder dimension row that has the natural key and "pending" attributes. When the real details arrive, you fill in that same row. The facts already point to it, so they need no update.

SQL Create an inferred member, then complete it

Press Run (or Ctrl + Enter) to see the result here.

This product dimension keeps only current values. In a Type 2 dimension, completing an inferred member usually overwrites the placeholder row, because "pending" was never a real historical state.

Placeholders suit cases where the code is real and details are only delayed. A mistyped code would also create a placeholder, so monitor how many stay incomplete.

DO NOTHING is right only if a repeated receipt line is a true duplicate. If sources can send corrections, you need an update rule or reversal rows, and silently ignoring changes would be wrong.

Late-arriving facts

A sale from last week may arrive today, for example from a store that was offline. Load it with its event date, and look up the Type 2 version that was valid then. Then ask:

  • Which summaries, snapshots and partitions must be recalculated?
  • Have reports for that period already been published?

A late-arriving dimension change is harder: you learn today that a customer moved two months ago. You may need to split an existing Type 2 version and repoint facts that happened after the true change date.

Two kinds of time

Time Question it answers
Event (valid) time When did it happen or become true?
Load (system) time When did the warehouse learn about it?

Storing both lets you explain why last month's report differs from the same report run today. Keeping two independent timelines for each record is called bitemporal modeling.

Rerunning a day: delete and insert

A robust pattern for batch facts is to replace a whole slice: delete the target day, then insert that day from staging, inside one transaction.

SQL Replace one day safely

Press Run (or Ctrl + Enter) to see the result here.

Run the three statements as often as you like and day two stays at two rows. But this requires staging to hold the complete day. If it holds only the late rows, you would delete good data. Check completeness before deleting. Many platforms also offer partition replacement for the same job.

MERGE

Standard SQL's MERGE updates matched rows and inserts unmatched ones in one statement. SQLite does not support it, so this is read-only. Details vary by platform.

SQL
-- Read-only: general MERGE shape; check your platform's syntax
MERGE INTO fact_sales AS t
USING stg_sales AS s
  ON t.receipt_no = s.receipt_no AND t.line_no = s.line_no
WHEN MATCHED THEN
  UPDATE SET amount = s.amount
WHEN NOT MATCHED THEN
  INSERT (receipt_no, line_no, product_key, amount)
  VALUES (s.receipt_no, s.line_no, s.product_key, s.amount);

Deduplicate the source first: several source rows matching one target row cause an error on many platforms. Also learn how your platform behaves when two jobs merge at the same time.

A checklist for each fact load

Before publishing a loaded batch, verify:

  1. Row accounting. Rows in equal rows loaded plus rows rejected or deliberately merged.
  2. Grain. No duplicate grain keys, including against earlier batches.
  3. Lookups. The share of unknown and inferred keys is within your normal range.
  4. Fan-out. No lookup produced more rows than it received.
  5. Totals. Key amounts match the staging totals after documented rules.

A sudden jump in unknown keys often means that a dimension load failed or ran after the fact load. The facts then load "successfully" while many of them are unlabeled.

Repairing unknown keys

Facts that were assigned to -1 do not fix themselves when the dimension record appears later, because the fact row stores only -1. To repair them you need the original natural key. Keep it in staging, in a holding table or as an extra column, so that a repair job can look up the right surrogate key and update the fact. This is one reason inferred members are attractive: the link is correct from the start.

Very large corrections

Updating many rows in a huge fact table can be slow and expensive on analytical platforms, where an update may rewrite whole blocks or files. It can be better to rewrite the affected partitions from corrected source data. Do the work in a separate table, check it, then swap it in. Keep the previous version until the checks and the downstream refreshes have succeeded, so you can go back.

Common mistakes

A key lookup that matches two dimension rows duplicates the fact. After each load, compare staging row counts and totals with what arrived in the fact table.

Check your understanding

Quick check · 4 questions

of 4 answered You scored / 4 · perfect!

  1. 1. What is an inferred member?

    It gives early facts a real key that can be completed later.
  2. 2. Why use a LEFT JOIN for dimension lookups in a fact load?

    An inner join would silently drop unmatched facts.
  3. 3. A late sale from last week arrives today. Which Type 2 version should it use?

    History is assigned by event time.
  4. 4. What must be true before delete-and-insert for a day is safe?

    Otherwise good rows are deleted and not replaced.

Summary

  • Fact loads translate natural keys into surrogate keys, using event dates for history.
  • Handle early facts with Unknown rows, holding or inferred members.
  • Record event time and load time, and refresh what late data affects.
  • Make reruns safe with unique grains, slice replacement or a carefully prepared MERGE.

Next: Analytical SQL for Warehouses

आपकी प्रगति इसी ब्राउज़र में सेव होती है। खाते की ज़रूरत नहीं है।