English · IndiaLog in
Rackwave Technologies
Request quote Free consultation
DataUpdated 26 Nov 2026

dbt Models: Materializations, Layers and Best Practice

dbt models: materializations, layers, ref, contracts and performance.

Written by Rohan Verma
  • Published25 Nov 2026
  • Read time6 min
  • Length1,260 words
Share this guide

What are dbt models? dbt models are SQL select statements, each saved in its own file, that dbt turns into tables or views in your warehouse. They are the core building block of a dbt project: every transformation, from cleaning a raw table to calculating revenue, is a model that can reference other models. dbt works out the order, runs them and lets you test and document each one. This guide explains how models work, the materializations available, how to use ref and configuration, how to structure staging, intermediate and mart models, how to optimise cost and performance and the mistakes that produce slow or tangled projects. Details vary by version and adapter, so confirm in current dbt documentation.

Short answer: Write each model as a single select, reference upstream models with ref, choose a materialization that fits size and freshness, keep models small and layered, and add tests and descriptions as you go.

How a model works

A model file contains a select statement and optional configuration. When you run dbt, it wraps the select in the correct create statement for your warehouse, replaces ref calls with the real table names and runs the models in dependency order. You never write create or insert statements yourself, which keeps logic readable and portable. See getting started with dbt and data modeling.

dbt models diagram
Figure 1. dbt models: source, staging, intermediate and how they relate.

Materializations compared

MaterializationWhat it buildsBest forTrade-off
ViewA database viewLight transformations, staging modelsRecomputed on every query
TableA full table rebuilt each runMarts queried oftenRebuild cost grows with data
IncrementalAdds or updates only new rowsLarge event or fact tablesMore logic, needs a good key. See incremental models
EphemeralInlined as a subquerySmall reusable logicNot queryable on its own
Materialized viewWarehouse-managed refreshed viewSupported platforms with frequent readsPlatform-specific behaviour

Set the default in project configuration and override per model.

Layering models

LayerPurposeRules
StagingOne model per source table, cleaned and renamedNo joins, light casting only, usually views
IntermediateReshapes and combines staging modelsReusable steps, not exposed to BI
MartsBusiness entities and factsTables, tested and documented, owned by a team

This keeps logic in one place, makes lineage easy to read and limits the impact of source changes to the staging layer.

Models at a glance summary
Figure 2. Models at a glance: staging, marts, materialization at a glance.

Writing good models

  1. Start from a clear question. Define the grain: what does one row represent?
  2. Select from refs and sources. Use ref for models and source for raw tables.
  3. Keep one purpose per model. Split long queries into named steps.
  4. Use common table expressions. Name each step so the query reads top to bottom.
  5. Configure the materialization. Set it in the file header or project file.
  6. Add tests and descriptions. Document the grain and key columns.

Configuration options worth knowing

  • Schema and alias settings to control where a model is built.
  • Tags to group models for selective runs.
  • Grants to manage who can read the output.
  • Contracts and constraints to enforce column types and shape.
  • Access modifiers to mark public and private models.
  • Pre and post hooks for small, controlled side effects.

Performance and cost

Models run on your warehouse, so poor SQL costs money. Filter early, select only needed columns, avoid unnecessary cross joins and use incremental builds for large tables. Review run times after each release, and use partitioning and clustering features of your platform where they help. Build only what changed in continuous integration using state selection, and avoid rebuilding the entire project for a small edit.

  • Filter and prune columns early.
  • Use incremental models for large facts.
  • Check query plans for the heaviest models.
  • Use selective runs in development.

Testing models at the right level

Every model should have tests that match its role. Staging models need key checks such as unique and not null, mart models need relationship and accepted-value tests and critical business metrics deserve custom checks against known totals. Run tests on every build and treat a failure as a blocker for downstream models, not an afterthought. See dbt tests for the full approach.

Add unit-style tests for complex logic using small fixed inputs and expected outputs, so refactoring can be done with confidence. Together these checks turn models from fragile scripts into dependable data products.

  • Key tests on every model.
  • Relationship tests between facts and dimensions.
  • Custom tests for critical metrics.
  • Unit tests for complex logic.

Handling slowly changing and late-arriving data

Facts often arrive late and dimensions change over time. Decide per model how to handle updates: overwrite, keep history with a snapshot or reprocess a trailing window in an incremental model. Writing the rule into the model description prevents confusion when numbers change after a rerun, and keeps reports consistent with business expectations. See snapshots.

Naming conventions that scale

Agree naming rules early and enforce them in review. Prefix staging models, name marts after the business entity, use singular names for dimensions and describe time grain in fact table names. Consistent names make lineage readable and let new team members find the right model quickly.

  • Prefix by layer.
  • Name marts after business entities.
  • Include grain in fact names.

Common mistakes

  • Models with several grains mixed together.
  • Long, unreadable queries with nested subqueries.
  • Hard-coded table names instead of ref and source.
  • Everything built as a table, even tiny lookups.
  • No descriptions or tests on mart models.
  • Business logic duplicated across several models.

A worked example

A team had one huge query that built a revenue report. They split it into staging models for orders and payments, an intermediate model that joined them at order grain and a mart for daily revenue. Staging stayed as views, and the mart became an incremental table.

Run time dropped sharply, a bug in payments was fixed in one place and lineage showed exactly how the report was built. The example is illustrative.

Best practices and support

  • Document the grain of every mart.
  • Use consistent naming and folder structure.
  • Review SQL in pull requests.
  • Track run time and cost per model.

Our data analytics service and data warehousing service design clean, efficient dbt model layers.

Read dbt tests, macros and Jinja, project structure and ETL pipelines for related topics.

Model quality checklist

ItemDone when
GrainStated in the description
RefsNo hard-coded table names
MaterializationChosen deliberately
TestsKeys and key rules tested
DocsColumns described
CostRun time reviewed

Model contracts and public interfaces

As projects grow, some models become interfaces used by other teams. Mark these as public, define contracts that fix column names and types and version them when you must make breaking changes. This lets producers change internals safely and gives consumers a stable target. Keep internal models private so that teams cannot depend on them by accident.

  • Mark shared marts as public.
  • Enforce contracts on public models.
  • Version breaking changes.
  • Keep internals private.

Refactoring and deprecation

Models accumulate. Review unused models regularly using lineage and query logs, deprecate them with a clear date and remove them once nothing depends on them. Refactor in small steps, comparing results before and after, so a change in structure never changes the numbers unexpectedly.

Key takeaways

  • A model is one select statement.
  • Use ref to build the graph.
  • Pick materializations deliberately.
  • Layer staging, intermediate and marts.
  • State the grain.

Quick glossary

TermMeaning
ModelSQL select built as table or view
MaterializationBuild strategy for a model
GrainWhat one row represents
ContractEnforced column definitions
MartBusiness-facing model

Read next: BigQuery vs Snowflake: Which Cloud Data Platform? · BigQuery Streaming Inserts: Real-Time Ingestion Guide · BigQuery Access Control: IAM, Policy Tags and Row Security

Frequently asked questions

What is a dbt model?

A SQL select statement in a file that dbt builds as a table or view in the warehouse.

What does the ref function do?

It references another model, resolves its real name and builds the dependency order.

Which materialization should I use?

Views for light staging, tables for marts, incremental for large facts and ephemeral for small helpers.

What is model grain?

The level of detail one row represents, such as one row per order.

Can models contain joins?

Yes, usually in intermediate and mart layers rather than in staging.

How do I speed up slow models?

Filter early, prune columns, use incremental builds and review the query plan.

What are model contracts?

Enforced definitions of column names and types for models used by others.

How do I organise models?

Use staging, intermediate and mart folders with consistent naming.

Was this guide helpful?Your answer helps us decide what to improve. It is not linked to you.

#dbt models#dbt materializations#dbt staging models#dbt marts#dbt ref function

About the author

Rohan Verma

Data Platforms Consultant

Rohan works across CDP, data warehouse, and analytics platform implementations, helping clients figure out which data infrastructure actually fits their scale rather than defaulting to whatever's currently trending.

Data Platforms & Technical Architecture

Ready to put AI and data to work?

Book a free consultation and leave with a plan, not a pitch.

Get a free consultationRequest quote