0
0
DbtHow-ToBeginner ยท 3 min read

How to Use dbt compile: Syntax, Example, and Tips

Use the dbt compile command to generate compiled SQL files from your dbt models without executing them on the database. This helps you check the final SQL code dbt produces for debugging or review.
๐Ÿ“

Syntax

The basic syntax of dbt compile is simple:

  • dbt compile: Compiles all models in your project.
  • dbt compile --select model_name: Compiles only the specified model.
  • dbt compile --profiles-dir path: Uses a custom profiles directory.

This command generates compiled SQL files in the target/compiled/ directory without running them on your database.

bash
dbt compile

dbt compile --select my_model

dbt compile --profiles-dir ./custom_profiles
๐Ÿ’ป

Example

This example shows how to compile a specific model named orders in a dbt project. It generates the compiled SQL file so you can review the final query dbt will run.

bash
dbt compile --select orders
Output
Running with dbt=1.4.6 Found 1 model, 0 tests, 0 snapshots, 0 analyses, 0 macros, 0 operations, 0 seed files, 0 sources 12:00:00 | Concurrency: 1 threads 12:00:00 | Compiling model orders 12:00:01 | Compilation complete Compiled SQL files are saved in target/compiled/your_project_name/models/orders.sql
โš ๏ธ

Common Pitfalls

Common mistakes when using dbt compile include:

  • Expecting dbt compile to run models and update the database. It only generates SQL files.
  • Not checking the target/compiled/ directory for the compiled SQL output.
  • Using dbt compile without selecting models when you want to compile only specific models, which can be slow on large projects.

Wrong usage example:

dbt compile --run
# This is invalid because --run is not a valid flag for compile

Right usage example:

dbt compile --select my_model
bash
dbt compile --run

# Correct:
dbt compile --select my_model
๐Ÿ“Š

Quick Reference

CommandDescription
dbt compileCompile all models without running them
dbt compile --select model_nameCompile only the specified model
dbt compile --profiles-dir pathUse a custom profiles directory
dbt compile --helpShow help for the compile command
โœ…

Key Takeaways

Use dbt compile to generate compiled SQL files without running models.
Compiled SQL files are saved in the target/compiled/ directory.
Use --select to compile specific models for faster feedback.
dbt compile does not update your database; it only prepares SQL.
Check for typos in command flags to avoid errors.