We are using dbt in combination with SQL Server 2019 and the usage of CTEs are a huge performance drag for us.
The main issue with the CTEs is, that they are deeply nested over several levels. E.g. CTE_L1 is refering to CTE_L2, CTE_L2 is referring to CTE_L3. The execution plan looks something like that and the same code is executed over and over again and most of the branches are including execution of the code of the lowest level over and over again:
Without dbt we would just use temporary tables in the temp db (#tbl_L1, #tbl_L2 etc) which then can be reused by lower levels. We tried that (w/o dbt) and it's much much faster than using the CTEs. Is there any way to utilize the temp db instead of CTEs? We are using that adapter for SQL server: https://github.com/dbt-msft/dbt-sqlserver
Materializing every level is not an option. The depth is dynamic and the number of tables is too high.