跳到正文

第 3 课 · Architecture

Warehouse Layers: Staging, Core and Marts

Understand data warehouse architecture: sources, landing and staging, the integrated core, data marts and the access layer, with a runnable SQL walkthrough.

A well-run warehouse is not one big pile of tables. Data moves through layers, and each layer has one job. Layers make problems easier to find: when a number looks wrong, you can check it step by step until you see where it changed.

Names vary between teams. Focus on the jobs, and pick names that your team will use consistently.

The usual flow

flowchart LR
  S["Sources"] --> L["Staging"]
  L --> C["Core (integrated)"]
  C --> M["Data marts"]
  M --> U["Dashboards and analysts"]

Data flows one way; each layer builds on the one before it.

Layer Job Who uses it
Sources Applications, files, APIs that create data Source system owners
Landing / staging Receive raw data with little change; record when it arrived Pipelines and engineers
Core / integration Clean, match and combine sources; keep history Engineers, some analysts
Data marts / presentation Business-friendly tables for a subject or team Analysts, BI tools
Access / semantic Shared metric definitions, views, permissions Everyone who reads

Some teams use the labels bronze, silver and gold for raw, cleaned and business-ready data. This "medallion" naming expresses the same idea of increasing refinement.

Staging: keep what you received

Staging holds data as close to the source as practical. Add a few technical columns, such as the load time, the source file or system, and a batch number. Avoid business rules here. If a rule turns out to be wrong, you can rebuild later layers from staging without asking the source to send everything again.

How long you keep raw data is a policy decision. It affects cost and privacy: raw data often contains personal details, so restrict who can read it.

Core: one version of each thing

The core layer does the hard work: removing duplicates, fixing types, mapping codes, matching the same customer across systems and keeping history. It is often modeled with a dimensional design, a normalized design or a Data Vault. The next lesson compares them.

Marts: shaped for readers

A data mart is a set of tables for one subject area, such as sales or support. Marts use friendly names and simple joins. A dependent mart is built from the core. An independent mart loads straight from sources; it is quick to start, but several independent marts tend to disagree with each other.

Walk a record through the layers

The next block builds all three layers for customer data from two systems. Notice that each step only fixes what belongs to it.

SQL From staging to a mart

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

Two things deserve attention. Matching people by email address is a matching rule, and it is only an assumption: shared or changed addresses break it. And when two rows tie on load date, the query breaks the tie by source name, which is arbitrary. In a real project you would agree on which system wins for each field and write that rule down.

Other architecture choices

  • Operational data store (ODS): an integrated, near-current copy used for operational reporting. It usually keeps little history.
  • Batch and streaming: some data arrives on a schedule, some continuously. Both can feed the same layers.
  • Environments: keep development, test and production separate so that experiments do not change reports people rely on.
  • Naming: prefixes such as stg_, dim_, fact_ or separate schemas show each table's layer.

Rules that keep layers useful

  1. Data flows forward. A staging table should not read from a mart.
  2. Readers use marts or the semantic layer, not staging.
  3. Each layer can be rebuilt from the layer before it, or you document why not.
  4. Business rules live in code under version control, not in someone's memory.

Trace a wrong number

Layers pay off when something breaks. Imagine that a manager says the customer count for Kenya is too low. Work backward:

  1. Mart. Run the mart query and confirm the number the manager sees.
  2. Core. Count Kenyan customers in the core table. If it matches the mart, the mart is not the cause.
  3. Staging. Look for rows whose country looks like Kenya. Perhaps a new spelling such as KENYA with a trailing space, or a code the CASE expression does not know, produced NULL.
  4. Source. If staging never received the rows, check the extract and the source system.

In our example, the CASE expression returns NULL for any unlisted country. That is a quiet failure. A better design keeps a mapping table of source values and standard codes, and a check that lists unmapped values after each load. Business users can then review the mapping without reading SQL.

Decide what each layer promises

It helps to write a short promise for each layer:

Layer A reasonable promise
Staging "This is what we received, with the time we received it. It may be messy."
Core "Types are valid, duplicates are handled and keys are consistent."
Marts "Names and definitions are agreed. Changes are announced before they happen."

Readers then know what they can depend on. If an analyst builds a report directly on staging, its columns may change without notice, because staging made no promise about them.

Layers and materialization

A layer is a logical idea. Each table in it might be a stored table, a view or a materialized view. Views are cheap to change and always reflect their inputs, but they repeat the work on every query. Stored tables cost storage and need loading, but read quickly and keep a stable result. Many teams use views for light cleanup and stored tables where transformations are heavy or history must be preserved. Remember that a view cannot keep history that its source tables have already lost.

Common mistakes

Adding layers "just in case" creates copies that nobody understands. Each layer should answer the question "What goes wrong if we remove it?" If you cannot answer, you may not need it.

Check your understanding

Quick check · 3 questions

of 3 answered You scored / 3 · perfect!

  1. 1. What is the main job of the staging layer?

    Staging preserves what was received, so later layers can be rebuilt.
  2. 2. What is a risk of several independent data marts?

    Without a shared integrated layer, each mart can apply its own rules.
  3. 3. Where should a rule that maps country names to codes normally live?

    Applying it once in the core keeps every consumer consistent and the rule reviewable.

Summary

  • Layers separate receiving, integrating and presenting data.
  • Staging stays close to the source; the core cleans, matches and keeps history; marts serve readers.
  • Matching and tie-breaking rules are business decisions that should be written down.
  • Flow data forward and keep layers rebuildable.

Next: Inmon, Kimball and Data Vault

你的学习进度保存在此浏览器中,无需账号。