<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by Sebastian Daum on Medium]]></title>
        <description><![CDATA[Stories by Sebastian Daum on Medium]]></description>
        <link>https://medium.com/@sebastian.daum89?source=rss-8930e633e531------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*dmbNkD5D-u45r44go_cf0g.png</url>
            <title>Stories by Sebastian Daum on Medium</title>
            <link>https://medium.com/@sebastian.daum89?source=rss-8930e633e531------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 23 Nov 2026 20:29:40 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@sebastian.daum89/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How to speed up single dbt invocations when using changing dbt variables]]></title>
            <link>https://medium.com/@sebastian.daum89/how-to-speed-up-single-dbt-invocations-when-using-changing-dbt-variables-b9d91ce3fb0d?source=rss-8930e633e531------2</link>
            <guid isPermaLink="false">https://medium.com/p/b9d91ce3fb0d</guid>
            <category><![CDATA[data-engineering]]></category>
            <category><![CDATA[dbt]]></category>
            <category><![CDATA[apache-airflow]]></category>
            <dc:creator><![CDATA[Sebastian Daum]]></dc:creator>
            <pubDate>Sun, 05 Jun 2022 21:38:51 GMT</pubDate>
            <atom:updated>2022-06-06T06:38:14.499Z</atom:updated>
            <content:encoded><![CDATA[<p>This post explains a potential speed improvement when using <a href="https://www.getdbt.com/">dbt </a>to run individual models instead of letting dbt run or build all or a subset of models in your dbt project and when each dbt invocation has changing dbt variables.</p><p><strong>You might ask why this is relevant?</strong> It really depends on how you use dbt and how dbt integrates with the existing tool stack.</p><p>At my company we use <a href="https://airflow.apache.org/">Apache Airflow</a> to orchestrate dbt and we have chosen to have a single Airflow task per dbt model. Each dbt model invocation usually has a set of external context variables, such as e.g. execution id, batch timestamp etc. Context variables are very important e.g. for specifing relevant time windows in your dbt transformations or a job ID that you want to include as descriptive information in your dbt models.</p><p>The main problem is when your dbt project is of reasonable size, like e.g. &gt;1,000 models, &gt;1,000 tests and many sources, macros, etc., and if you provide changing dbt variables. Each dbt invocation requires a full dbt project parsing, which takes up to 2–3 minutes before the first SQL statement of your model reaches your target database (except for dbt metadata queries to build the dbt cache). That’s an enormous amount of extra work. However, there is a neat trick to reduce the dbt init time to 14–26 seconds depending on your hardware running dbt and I/O performance.</p><ol><li><strong>How we orchestrate dbt with Apache Airflow</strong></li></ol><p>For each dbt project we set up a CI/CD pipeline that runs after the main branch has been changed and that produces a complete, compiled dbt project with all target files and all required dbt artifacts such as manifest.json, catalog.json, run_results.json, and sources.json. Then we persist the complete project with all generated files on Azure Blob Storage and create a new dbt container image in which all created dbt files are copied. This image is stored in our cloud container registry connected to our Kuberentes cluster (AKS in our case).</p><p>We use a custom Airflow Operator “DbtK8SOperator” that extends the functionality of the existing <a href="https://airflow.apache.org/docs/apache-airflow-providers-cncf-kubernetes/stable/operators.html">KubernetesPodOperator</a>. The KubernetesPodOperator allows you to create and run a custom pod on your Kubernetes cluster. Also, we have a dynamic Airflow DAG that reads the created manifest.json from a mounted Azure file share and creates one or more Airflow tasks per DBT model. One or more since we have custom freshness checks or other tasks as preconditions before different parts of the dbt DAG can run.</p><p>An Airflow task instance looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/583/1*QM7YQpQGewaGmdHvaiD25w.png" /><figcaption>Creating Airflow task instance per dbt model</figcaption></figure><p>At the moment we have one Airflow DAG per dbt project. And our biggest dbt project has the following statistics:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/1024/1*p-JR9myMDPaE0s5bAq6F_A.png" /><figcaption>dbt project statistics</figcaption></figure><p>1779 models, 4680 tests, 860 macros, 3 operations, 18 seed files, 347 sources. In the end we have an Airflow DAG with over 2,000 tasks. The DbtK8SOperator starts a new Kubernetes pod and runs the following command:</p><pre>dbt --debug build --vars &#39;{&quot;audit_id&quot;: &quot;1732991&quot;, &quot;batch_ts&quot;: &quot;2022-06-04T00:00:00+02:00&quot;}&#39; --select RDV_XXX_HUB</pre><p>As you can see we use dbt variables to pass external context such as audit_id or batch_ts to our dbt tranformation. However as both variable are different than the variables at compile time in our CI/CD process, dbt will trigger a full re-parse of the project that needs up to 2–3 minutes before the actual dbt tasks can run.</p><p><strong>2. Understand why dbt triggers a full re-parse when dbt variables change</strong></p><p>Let’s use the dbt cli to run a dbt compile command on a model and run it with different input variables:</p><pre>time dbt compile -s PSA_XXX_RAW --vars &#39;{&quot;audit_id&quot;: &quot;1732992&quot;, &quot;batch_ts&quot;: &quot;2022-06-04T00:00:00+02:00&quot;}&#39;<br>13:47:09  Running with dbt=1.0.6<br>...<br>13:49:23  Found 1779 models, 4680 tests, 0 snapshots, 0 analyses, 860 macros, 3 operations, 18 seed files, 347 sources, 0 exposures, 0 metrics<br>13:49:23  <br>13:49:33  Concurrency: 20 threads (target=&#39;dev&#39;)<br>13:49:33  <br>13:49:35  Done.</pre><pre>real    2m30.165s<br>user    2m26.238s<br>sys     0m6.634s</pre><p>Let’s run the same command with a another audit_id:</p><pre>time dbt compile -s PSA_XXX_RAW --vars &#39;{&quot;audit_id&quot;: &quot;1732991&quot;, &quot;batch_ts&quot;: &quot;2022-06-04T00:00:00+02:00&quot;}&#39;<br>13:49:57  Running with dbt=1.0.6<br>13:49:59  Unable to do partial parsing because config vars, config profile, or config target have changed</pre><pre>...<br>13:52:13  Found 1779 models, 4680 tests, 0 snapshots, 0 analyses, 860 macros, 3 operations, 18 seed files, 347 sources, 0 exposures, 0 metrics<br>13:52:13  <br>13:52:24  Concurrency: 20 threads (target=&#39;dev&#39;)<br>13:52:24  <br>13:52:26  Done.</pre><pre>real    2m31.803s<br>user    2m28.116s<br>sys     0m6.341s</pre><p>As you can see, if at least one of the configuration variables, the config profile, or the config target has changed, dbt accurately tells that it cannot partially parse the dbt project. It doesn’t know this by comparing the actual variables, but by storing their combined hash values ​​in a file partial_parse.msgpack and then comparing the current variables hash value to those in the file.</p><p>In a way, it makes perfect sense for dbt to do a full re-parse when dbt variables change, since dbt variables can be used for almost anything. With dbt variables you can enable, disable models, you can refer to other models in ref() depending on variables, etc. It would be really difficult for the dbt parser to know exactly if a dbt variable only changes context (e.g a different audit id) or where maybe it changes the dbt DAG itself or model dependencies or more.</p><p><strong>3. Make dbt think the variables haven’t change</strong></p><p>In our setup we don’t use dbt variables at runtime to change dbt DAG drastically, e.g. dynamically enable or disable models, change model dependencies, etc. After the final build in our CI/CD setup, only the context information changes.</p><p>And if dbt doesn’t really store the actual values ​​of the variables but their hashed representation, the only thing we really need is to tell dbt that nothing has changed, right?</p><p>We looked at the dbt-core source code and found the relevant logic that determines whether partial parsing is possible and whether dbt variables have changed. The logic is simple:</p><ol><li>Open the partial parsed representation of your dbt project’s dbt manifest stored in partial_parse.msgpack</li><li>Read stored variable hash representation of dbt variable, profile and target if provided by dbt invocation at last dbt compile time</li><li>Compare it to the current hash representation of the dbt command</li><li>If the hash representation is not the same, trigger a full dbt project analysis</li></ol><p>The dbt-core method that create the hash looks like this:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/722/1*ppqQyFZTdr1_XRlBDrbAjQ.png" /></figure><p>Therefore, the only thing that might be required is to open the partial_parse.msgpack file, overwrite the hash representation with the current hash value of your dbt variables, and we should see a drastic speed improvement.</p><p>Internally we have written a small Python cli “dbtturbo” that we run before our dbt command runs. The most important step is the write_manifest_cache method:</p><figure><img alt="" src="https://cdn-images-1.medium.com/max/576/1*Z6eYeI55F-k6CVlrxavqvA.png" /></figure><p>It is part of an abstract class that implements common logic for all representations by opening the dbt file partial_parse.msgpack, reading the hash representation, computing the new hash representation of the current dbt invocation and storing it again in the file partial_parse.msgpack. We’re also changing the path targets for our dbt seeds as well, since the paths result from the CI/CD pipeline and need to be adjusted as well.</p><p>Now let’s run the same dbt compile command above again with a new audit_id, but first let’s use dbtturbo to set the new hash representation:</p><pre>time (dbtturbo dbtvars setvars --logging-level INFO --vars &#39;{&quot;audit_id&quot;: &quot;1732995&quot;, &quot;batch_ts&quot;: &quot;2022-06-04T00:00:00+02:00&quot;}&#39; &amp;&amp; dbt compile -s PSA_XXX_RAW --vars &#39;{&quot;audit_id&quot;: &quot;1732995&quot;, &quot;batch_ts&quot;: &quot;2022-06-04T00:00:00+02:00&quot;}&#39;)<br>[2022-06-05 19:05:46,625-dbtturbo-INFO-dbt_cache_processor.py] Start running dbt cache extracting<br>[2022-06-05 19:05:47,070-dbtturbo-INFO-dbt_cache_processor.py] Done dbt cache extracting<br>[2022-06-05 19:05:47,070-dbtturbo-INFO-dbt_cache_processor.py] Start calc dbt hash from vars input: {&quot;audit_id&quot;: &quot;1732995&quot;, &quot;batch_ts&quot;: &quot;2022-06-04T00:00:00+02:00&quot;}<br>[2022-06-05 19:05:47,070-dbtturbo-INFO-dbt_cache_processor.py] Done calc dbt hash<br>[2022-06-05 19:05:47,070-dbtturbo-INFO-dbt_cache_processor.py] Start running dbt cache writing<br>[2022-06-05 19:05:47,070-dbtturbo-INFO-base.py] Current vars checksum: ac5f24462a136fb0eaca1ab22650b9bf44db6599ae8d1595631753b3e0d16307<br>[2022-06-05 19:05:47,071-dbtturbo-INFO-base.py] New vars checksum: ac5f24462a136fb0eaca1ab22650b9bf44db6599ae8d1595631753b3e0d16307<br>...<br>[2022-06-05 19:05:47,250-dbtturbo-INFO-utils.py] &#39;dbtturbo.dbt_cache_processor.run&#39; took 0.6251 seconds to execute<br>19:05:50  Running with dbt=1.0.6<br>19:06:01  Found 1779 models, 4680 tests, 0 snapshots, 0 analyses, 860 macros, 3 operations, 18 seed files, 347 sources, 0 exposures, 0 metrics<br>19:06:01  <br>19:06:10  Concurrency: 20 threads (target=&#39;dev&#39;)<br>19:06:10  <br>19:06:12  Done.</pre><pre>real    0m26.560s<br>user    0m25.315s<br>sys     0m4.045s</pre><p>Great! So we can speed up the dbt call time from 2m31 to 0m26, which is a significant improvement in start time. In our production setup, we sometimes see startup times of 0m14 seconds when running dbt on a Kubernetes node without much noise.</p><p><strong>4. Can we go even faster?</strong></p><p>dbt-core 1.1.0 adds an experimental --cache-selected-only option that looks very promising as it only caches schema objects of selected models. In theory, this should speed up startup time even further, as it reduces the number of metadata queries that are run for cache generation. But it’s only available for the postgres adapter, so we couldn’t test it yet.</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=b9d91ce3fb0d" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>