# I built every Databricks table type. Here is what I would keep

When I came to Databricks, tables and views were familiar, but streaming tables, AUTO CDC, materialized views, metric views and dynamic views were not. I wanted to know whether they would save me work, or whether I'd be better off writing the SQL myself like in any other warehouse.

So I built a small booking project on `samples.wanderbricks`, used every type, and compared each one with the same job in plain SQL.

## The project

A holiday rental company records a change each time a booking is created, confirmed, changed or cancelled. Some changes come twice, some come late, and a few have an amount of zero. Guest operations wants to see cancellations within seconds, while every other team is fine with yesterday's numbers.

The raw changes go into bronze, silver cleans them, and gold has a table or view for each team.

![One feed through bronze, silver and gold, with live and daily objects and who uses them](https://raw.githubusercontent.com/mloh-studio/mloh/refs/heads/main/databricks-tables-views-pipelines/images/01-whole-picture.svg align="center")

![What each type stores: managed table, view, materialized view, streaming table, metric view and dynamic view](https://raw.githubusercontent.com/mloh-studio/mloh/refs/heads/main/databricks-tables-views-pipelines/images/02-tables-vs-views.svg align="center")

## Streaming tables: yes, for big or live sources

A streaming table reads only the rows that are new in its source since the last refresh and appends them. It keeps track of which versions of the source table it has already read, so it never reads the same write twice. If you know Kafka, think of a consumer that writes to a table. The source is the topic, and the record of what it has read is the consumer offset.

Without it, I'd write a `MERGE` that inserts each row whose key isn't in the target yet. That never misses a row and drops duplicates too, but every run compares the whole source with the whole target. That's fine for a daily job on a small table, and too slow when the source is large or you want new rows within seconds.

For example, this copies new booking changes and drops rows with a zero amount:

```sql
CREATE OR REFRESH STREAMING TABLE booking_updates_clean AS
SELECT * FROM STREAM rental_bronze.booking_updates
WHERE total_amount > 0;
```

Unlike the `MERGE`, it doesn't remove duplicates, so if the source gets the same row twice, the streaming table gets it twice too. You need an extra step to drop them.

## AUTO CDC: optional

AUTO CDC turns a list of changes into a table with one row per key. You tell it the key and the column that orders the changes. It then keeps the latest version of each key, or every version with a start and end time. For example, this keeps the latest version of each booking:

```sql
CREATE OR REFRESH STREAMING TABLE bookings;

CREATE FLOW bookings_cdc AS AUTO CDC INTO bookings
FROM STREAM(booking_updates_clean)
KEYS (booking_id)
SEQUENCE BY STRUCT(updated_at, booking_update_id)
STORED AS SCD TYPE 1;
```

Its main job is applying changes in the right order, even when they arrive late. A `MERGE` can do that too, if it only updates a row when the incoming change is newer. AUTO CDC saves you writing that condition.

If the source keeps every change, you don't need it. A view or materialized view that picks the latest change per key gives the same table, late changes included.

When the source deletes old changes or has too many to read every day, you update the table with only the new changes instead. A `MERGE` into a normal table still does that, and AUTO CDC just saves you writing and testing it. Writing it yourself also lets you add a rule AUTO CDC can't follow, such as "a cancelled booking stays cancelled".

## Materialized views: yes, when full rebuilds get slow

A materialized view stores the result of a query and refreshes it for you, much like a dbt `table` model. Where it can, a refresh only recomputes what changed. For example, this counts booking changes per day:

```sql
CREATE OR REFRESH MATERIALIZED VIEW daily_booking_activity AS
SELECT CAST(updated_at AS DATE) AS activity_date, destination, status,
       count(*) AS changes, sum(total_amount) AS total_amount
FROM booking_changes_enriched
GROUP BY ALL;
```

A scheduled `CREATE OR REPLACE TABLE ... AS SELECT` gives the same result, but it rebuilds the whole table every run. That's fine while the table is small. Once a rebuild gets slow, the materialized view can refresh only what changed, and you don't have to rewrite the query as a `MERGE` to get there.

## Metric views: yes

A metric view is the semantic layer inside Databricks, like the dbt Semantic Layer or LookML. You define each metric one time, and every dashboard, Genie space and SQL query uses that same definition:

```sql
CREATE VIEW booking_metrics WITH METRICS LANGUAGE YAML AS $$
version: 0.1
source: SELECT b.*, l.destination, l.country FROM bookings b JOIN property_listings l USING (property_id)
dimensions:
  - name: destination
    expr: destination
  - name: country
    expr: country
measures:
  - name: cancellation_rate
    expr: COUNT(1) FILTER (WHERE status = 'cancelled') / COUNT(1)
$$;

SELECT country, MEASURE(cancellation_rate) FROM booking_metrics GROUP BY country;
```

Each query picks its own grouping, such as country here, and the metric view works the formula out from the rows. That also keeps rates and averages right at every level, which is easy to get wrong when each report writes its own SQL.

## Dynamic views: no

A dynamic view shows different rows or columns depending on who runs the query, using functions such as `is_account_group_member` to check the user. For example, this masks emails for everyone outside the support admins group:

```sql
CREATE VIEW users_for_support AS
SELECT user_id, country,
       CASE WHEN is_account_group_member('support_admins') THEN email
            ELSE regexp_replace(email, '^[^@]+', '***') END AS email
FROM samples.wanderbricks.users;
```

The access rule lives inside the view's SQL, and permissions only show who can open the view, not what each person sees. A wrong join or a `SELECT *` can leak data, and if you test as an admin, you won't notice.

I'd put the rule on the table instead, with a row filter and a column mask, so every query follows it. For Delta Sharing, I'd give each recipient its own view or table, so a mistake only affects one recipient.

## Do you need a pipeline?

Not always, because you can also create streaming tables and materialized views on their own in Databricks SQL, each with its own schedule.

A pipeline helps when several tables must be built together, in the right order, and then tested. It can also run nonstop, which you need when new rows must show up within seconds. For one or two simple tables, though, I'd skip it.

![Two pipelines and the jobs that run them](https://raw.githubusercontent.com/mloh-studio/mloh/refs/heads/main/databricks-tables-views-pipelines/images/03-pipelines-vs-jobs.svg align="center")

## What I would keep

| Type | Keep it? | Why |
| --- | --- | --- |
| Streaming table | Yes, for big or live sources | It reads only new rows, so it stays fast enough to run every few seconds. |
| AUTO CDC | Optional | It saves you writing a `MERGE`. While every change is kept, one query does the same. |
| Materialized view | Yes, when full rebuilds get slow | It can refresh only what changed, with the same query. |
| Metric view | Yes | One definition of each metric for every tool. |
| Dynamic view | No | Row filters and column masks are safer. |

I keep a type when it does work that's hard to get right myself. When plain SQL does the same job with less to learn, I skip it.

## Try it

The code and setup steps are on [GitHub](https://github.com/mloh-studio/mloh/tree/main/databricks-tables-views-pipelines).

## Further reading

This post is a quick look at how each type differs from plain SQL, not a full guide, so it leaves out many features and tradeoffs. Before you choose a type, read its docs and test it against your own use case.

- [Streaming tables](https://docs.databricks.com/aws/en/ldp/concepts/streaming-tables)
- [AUTO CDC](https://docs.databricks.com/aws/en/ldp/cdc)
- [Materialized views](https://docs.databricks.com/aws/en/ldp/concepts/materialized-views)
- [Metric views](https://docs.databricks.com/aws/en/uc-semantics/metric-views)
- [Row filters and column masks](https://docs.databricks.com/aws/en/data-governance/unity-catalog/filters-and-masks/)
