Optimizing DBT Modeling Code: A Guide to DBT Setup and Hand-on commands
Tags: dbt, datascience, database, analytics
Published: 2023-02-17 13:17:33

## 1.	What is [DBT](https://www.getdbt.com/)?
[DBT](https://www.getdbt.com/) (Data Build Tool) is an open-source tool used for data modeling, data transformation, and data management. It is designed to help data teams build, maintain, and document their data models and transformations in an organized and efficient manner.

DBT helps automate data processing pipelines and provides a framework for modeling data in a declarative way. It supports a variety of databases, including Amazon [Redshift](https://aws.amazon.com/ru/redshift/), [Google BigQuery](https://cloud.google.com/bigquery/), [Snowflake](https://signup.snowflake.com/), and more, making it a versatile tool for managing data in various environments. 

## 2.	What code editor is better for DBT? 
There are several code editors that can be used for DBT, however, the most popular ones are Sublime Text and Visual Studio Code (VS Code). Here is a comparison between the two:


![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cfxi1nz6qyt89yf0i53a.png)

I personally use VS code and I find it very useful. All in all, a combination of DBT and VS Code make a suitable environment for developing and maintaining high-quality data models with powerful debugging and testing capabilities,.

## 3.	Setting up DBT with a data warehouse

**Step 1: Install DBT**
 DBT is a command-line tool that can be installed using pip, a Python package manager. You can install DBT by running the following command in your terminal: 

`pip install dbt`

**Step 2: Set up your data warehouse**
You will need to set up a data warehouse, such as Amazon Redshift, Google BigQuery, or Snowflake, to store your data. If you do not have a data warehouse set up yet, you will need to create one and upload your data onto it.

**Step 3: Configure your data warehouse connection**
In order to connect DBT to your data warehouse, you will need to create a profile in your dbt_project.yml file. This profile should include the connection details for your data warehouse, such as the host, database name, username, and password.

**Step 4: Create a DBT project**
You can create a new DBT project by running the following command in your terminal: 
`dbt init`
This will create a new directory with a basic structure for a DBT project, including a `dbt_project.yml` file, which you can use to configure your project.

**Step 5: Define your models**
DBT models are defined using SQL and you can use them to perform data transformations and modeling tasks. You can define your models in .sql files in the model’s directory of your DBT project.

**Step 6: Run and test DBT**
Once you have defined your models, you can run DBT by running the following command in your terminal:
`dbt run`
This will compile your models and run any tests you have defined. If there are no errors, your models will be deployed to your data warehouse.

Over time, you may need to make changes to your models or add new models. You can use DBT to manage these changes, track the state of your data models, and ensure that your data remains accurate and up-to-date.

## 4. Some hand-on commands you should know for building your model 

**- Create a new branch** 
`git checkout -b [new_branch_name] `
In some cases, if your company is using Jira or any product management tool, you can set up a link to the tickets with your PR by including ticket numbers in the names of your new branches. This setup would enable tickets to immediately link with the PR and update other stakeholders on the progress of your changes.
For example: I have ticket `spend-123` to solve the bug of transaction data. I would create a new branch by command: 
`git checkout -b spend-123_transaction_debug `

**- Switch and update the branch**  
You can switch your branch to master in order to update it along with the latest changes in your pipeline 
`git checkout master `
 
![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/cb05kyjj5244hp2mbsgm.png)

`git pull `

![Image description](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/hgbzc5ouh9m3l1debo0o.png)

To switch back to your branch run
`git checkout [branch_name] `


**- Running your model**
Run your built model 
`dbt run -m [model_name] `
OR target any dataset. 
For example, to run a model in preprod only, execute the following command:
`dbt run -m [model_name] -t preprod`

**- To test your built model, execute**
`dbt test --models snapshot_core_transactions` 

**- Reset your branch with this command **
`git reset --hard`

**- SQL fluff fix **
SQL fluff refers to redundant or unnecessary code in SQL statements. It can come in various forms, such as unnecessary parentheses, redundant clauses, or excessive spaces. The presence of SQL fluff can make the code more difficult to read, maintain, and optimize, leading to slower performance and increased complexity.
When mondeling data with DBT, it is important to write clean, efficient, and well-structured SQL code to make sure that your data models perform well and are easy to maintain. By reducing or removing SQL fluff you can simplify your code and make it easier to understand and maintain.
In some cases, we can use this command to do a quick fix by implementing a specific rule specified in your run: 
`sqlfluff fix models/[folder_name]/model_name.sql –rules [rule_name] `

**- Commit and push the request **
`git commit -m ‘message of your commit’ `
`git push` 
Copy your link URL after DBT creates your pull request and paste it. This would let your link to be submitted to your PR and fill more detailed information and requirements of your PR. 

In short, these are some basic tips to set up and run your model with DBT tool.