0
0
dbtdata~5 mins

Model contracts and access controls in dbt - Time & Space Complexity

Choose your learning style9 modes available
Time Complexity: Model contracts and access controls
O(n)
Understanding Time Complexity

When using model contracts and access controls in dbt, it is important to understand how the checks and permissions affect the time it takes to run your models.

We want to know how adding contracts and controls changes the work dbt does as data grows.

Scenario Under Consideration

Analyze the time complexity of the following dbt model with a contract and access control.


-- model.sql
{{ config(
  contracts=["not_null", "unique"],
  access_control={"role": "data_analyst"}
) }}

select * from source_table

This model selects all data from a source table, applies contracts to check data quality, and restricts access by role.

Identify Repeating Operations

Look at what repeats when dbt runs this model.

  • Primary operation: Scanning all rows in source_table to apply contracts.
  • How many times: Once per model run, but the scan covers every row in the table.
How Execution Grows With Input

The time to check contracts grows as the number of rows in source_table grows.

Input Size (n rows)Approx. Operations
1020 checks
100200 checks
10002000 checks

Pattern observation: The work increases directly with the number of rows and the number of contracts, so doubling rows doubles the checks.

Final Time Complexity

Time Complexity: O(n)

This means the time to run the model and check contracts grows linearly with the number of rows in the source table.

Common Mistake

[X] Wrong: "Adding access controls will make the model run slower by a lot."

[OK] Correct: Access controls usually only check permissions once per run and do not scan data rows, so they add very little time compared to contracts.

Interview Connect

Understanding how data checks and permissions affect run time helps you design efficient data models and explain your choices clearly in real projects.

Self-Check

"What if the model had multiple contracts checking different columns? How would the time complexity change?"