Selecting Part of the DAG

Concepts covered: Node selection

Unless told otherwise, dbt run builds every model in the project, as does dbt build. That suits the daily production build. It wastes time while you work on one model in a project of 400, where checking a change to 2 tables can mean waiting on a full build. The --select flag, -s for short, limits a command to the nodes it names, and a + on either side of a name extends the selection along the lineage.

The project used here

Each arrow runs from a node to a model that selects from it. fct_orders joins both staging models, and daily_revenue groups fct_orders by order date.
  • raw.orders (source, raw table)
  • raw.customers (source, raw table)
  • stg_orders (transform, table model)
  • stg_customers (transform, table model)
  • fct_orders (transform, table model)
  • daily_revenue (transform, table model)
  • raw.orders -> stg_orders
  • raw.customers -> stg_customers
  • stg_orders -> fct_orders
  • stg_customers -> fct_orders
  • fct_orders -> daily_revenue

A + before or after a name follows the lineage

A plain name selects that model and nothing else. A + before the name adds every ancestor up to the sources, and a + after it adds every descendant down to the last model. Ancestors include parents of parents, not only the models one step up.

CommandModels built
dbt run --select fct_ordersfct_orders
dbt run --select +fct_ordersstg_orders, stg_customers, fct_orders
dbt run --select fct_orders+fct_orders, daily_revenue
dbt run --select +fct_orders+stg_orders, stg_customers, fct_orders, daily_revenue
dbt run --select stg_orders+stg_orders, fct_orders, daily_revenue

dbt still runs whatever is selected in ref() order: +fct_orders builds both staging models before fct_orders, and stg_orders+ leaves stg_customers out because nothing flows from stg_orders into it.

An unselected parent keeps its last build

Selection decides which tables get rebuilt. A selected model still reads its parents, and a parent left out of the selection holds whatever its last build put there. The animation plays a full run, inserts order 5 into raw.orders, then runs 3 selections.

select
    o.order_id,
    o.order_date,
    o.amount,
    c.region
from {{ ref('stg_orders') }} as o
join {{ ref('stg_customers') }} as c
    on o.customer_id = c.customer_id
  1. dbt run
  2. dbt run --select fct_orders
  3. dbt run --select +fct_orders
  4. dbt run --select fct_orders+
A full run, then an order inserted and 3 selections: the model alone, with its parents, with its children.
StepCommandfct_orders rowsdaily_revenue on 2024-07-03
1dbt run490 revenue, 1 order
2order 5 inserted, then dbt run --select fct_orders490 revenue, 1 order
3dbt run --select +fct_orders590 revenue, 1 order
4dbt run --select fct_orders+5145 revenue, 2 orders

Step 2 is the one that catches people out. fct_orders was rebuilt after order 5 landed in raw.orders, yet it still has 4 rows. fct_orders reads stg_orders, a table last built in step 1, before order 5 arrived. Step 3 adds the parents, so stg_orders picks up order 5 and fct_orders reaches 5 rows. daily_revenue was not selected, so it still reports 90 for 2024-07-03. Step 4 adds the children, and daily_revenue shows 145 from 2 orders that day.

Choosing a selection

dbt build --select follows the same rules and also runs the tests on the selected models, each test right after the model it checks, so a failing test still stops that model's selected children.

Writing a selector comes down to asking where the stale tables are. If they sit above the model, because new data arrived in the sources, the + goes before the name. If they sit below it, because you changed the model's SQL, the + goes after it. When both are stale, use both.

Practicing selection

Pick a model in a project you know. Before running anything, write down which models the bare name should build. Do the same with + in front of the name and with + behind it, then with both together. Then run dbt ls --select with each of the 4 forms. It prints every node a selector matches, including sources and tests, without building any of them, so each wrong guess shows up next to the right list.

About this lesson

This section is part of the Deploying dbt: Beginner lesson on DataDriven, the home of data engineering. Lessons cover SQL, Python, Spark, data modeling and pipeline architecture, and each section pairs its explanation with worked examples and practice you run as you go.