Sub topic 10
Gold: tables shaped for business consumers
This chapter assumes you know basic SQL, can read PySpark, and have already built the bronze and silver layers from the earlier chapters. You should have a typed, deduplicated table at main.silver.orders.
What gold is for
The gold layer holds tables built for a specific business use: a dashboard, a report, a machine learning feature set, or a data product shared with another team. Silver answers "what happened, cleanly." Gold answers "what does this team need to see."
Gold tables are usually:
- Aggregated, for example daily revenue instead of one row per order.
- Joined across subjects, for example orders with customer attributes.
- Named in business terms, with columns an analyst can read without a data dictionary.
- Narrow in purpose. One consumer or question per table is easier to keep correct than one table for everyone.
Build a gold table
The example below turns cleaned orders into a daily revenue summary. It filters to completed orders, groups by calendar date, and counts and sums.
CREATE OR REPLACE TABLE main.gold.daily_revenue AS
SELECT
CAST(order_ts AS DATE) AS order_date,
COUNT(*) AS orders,
SUM(amount) AS revenue
FROM main.silver.orders
WHERE status = 'completed'
GROUP BY CAST(order_ts AS DATE)Three choices in this query are worth noticing.
- The business rule
status = 'completed'lives here, not in silver. Silver keeps every order, so another gold table can count cancellations without re-reading bronze. - The grain is one row per
order_date. State the grain of every gold table in its comment or documentation, because it decides which questions the table can answer. CREATE OR REPLACE TABLErebuilds the whole table from silver on each run. That is simple and always consistent, and it suits tables that are small relative to silver.
Full rebuild or incremental
A full rebuild recomputes everything. It cannot drift from silver, and late or corrected silver rows are picked up automatically. The cost grows with silver, so a rebuild that takes minutes today may take hours next year.
When a rebuild becomes too slow, there are two common options:
- Recompute only recent partitions, such as the last seven days, and merge them into the gold table with
MERGE. - Use a materialized view or a declarative pipeline table, and let Databricks decide how to update it incrementally. A later chapter covers this.
Incremental updates only stay correct if you recompute every group a change could touch. If an order from three days ago is corrected in silver, your job must refresh that day too.
Shape gold for the reader
Gold is where you pay for convenience, so make queries cheap and obvious for the people using it.
| Choice | Effect on consumers |
|---|---|
| Pre-aggregate to the reporting grain | Dashboards scan far fewer rows. |
| Denormalize dimensions such as customer region | Analysts avoid repeated joins. |
| Use clear names (`revenue`, not `amt_sum`) | Fewer misread metrics. |
| Add table and column comments | Catalog search and BI tools show them. |
Common mistakes
- Mixing grains. A table with both order-level and daily rows produces double counts when summed.
- Hidden filters. If
daily_revenueonly counts completed orders, say so in the table comment. Otherwise readers compare it with another source and find a gap. - Too many near-duplicate tables. Before adding a new gold table, check whether an existing one already has the right grain.
- Skipping checks. Compare a gold total against silver after a run, for example total revenue for one day. A mismatch usually means a filter or join changed the row count.
Exercise
The finance team wants monthly revenue by customer region, and main.silver.customers has customer_id and region. Which grain would you choose for the gold table, and which silver tables would the query read?
Show answer
Choose one row per month and region. The query joins main.silver.orders to main.silver.customers on customer_id, filters to completed orders, and groups by the month of order_ts and by region. Write it as CREATE OR REPLACE TABLE main.gold.monthly_revenue_by_region AS ....
Exercise
Why is it better to put the filter status = 'completed' in the gold query than in the silver transformation?
Show answer
Silver should keep all valid orders so that different gold tables can apply different business rules, such as one for completed revenue and one for cancellations. A filter in silver would remove data that other consumers need.