I found out a way to handle the temp tables in DBT, write all those in pre-hook and call the final temp table in the outside of the pre-hook, tested and is working fine, able to reduce the code running time from more than 20 mins to 1 min. But I see one problem that we can't see the lineage graph in the DBT documents. Is there any way to handle the temp tables other than pre-hook and with lineage in Docs?
Source tables in dbt There are no create or replace statements written in model statements. This means that dbt does not offer methods for issuing CREATE TABLE statements which can be used for source tables.
Yes, you can. SSRS simply executes the SQL you put in the query window, or the SP you put in. If that SQL creates and uses a temporary table, then SSRS will make use of it.
To create a temporary table, you must have the CREATE TEMPORARY TABLES privilege. After a session has created a temporary table, the server performs no further privilege checks on the table. The creating session can perform any operation on the table, such as DROP TABLE , INSERT , UPDATE , or SELECT .
Looking at SQL Profiler results from these queries (each were run 10 times and averages are below) we can see that the CTE just slightly outperforms both the temporary table and table variable queries when it comes to overall duration.
You're right in thinking that dbt does not support temporary tables. That's because temporary tables only persist in a single session, and dbt opens one connection/session per thread. Therefore any temporary tables created on one thread would not be visible to a model running on a different thread.
It sounds like CTEs are a performance drag for you though — out of interest, which warehouse are you using?
You've identified two workarounds, and there's another one worth discussing:
Option 1: Materialize your model as CTEs using the ephemeral
materialization (docs)
Pros:
ref
-ing themCons:
Option 2: Use pre-hooks to create temp tables
I would generally recommend against this — you can't test or document your models, and they won't be in the lineage graph (as you've noted).
Option 3: Materialize these models as tables in a separate schema, and drop the schema at the end of a run
I think Michael's suggestion is a good one! I'd tweak it just a little bit:
{{ config(
materialized='table',
schema='my_temporary_schema'
) }}
on-run-end
hook (docs) to drop that schema — in your dbt_project.yml
:on-run-end: "drop schema my_temporary_schema cascade"
Pros:
Cons:
drop cascade
command! This introduces fragility into your project!If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With