From 5a67e414b3f73d8e53974f3424639f3cc034bfef Mon Sep 17 00:00:00 2001 From: Joe Van Dyk Date: Fri, 8 Jun 2018 11:51:06 -0700 Subject: [PATCH] use a subselect instead of a CTE when building incremental models (must faster on postgresql due to the CTE optimization fence) --- .../macros/materializations/incremental/incremental.sql | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/dbt/include/global_project/macros/materializations/incremental/incremental.sql b/dbt/include/global_project/macros/materializations/incremental/incremental.sql index 71945f29f08..37d6837543a 100644 --- a/dbt/include/global_project/macros/materializations/incremental/incremental.sql +++ b/dbt/include/global_project/macros/materializations/incremental/incremental.sql @@ -60,10 +60,8 @@ {%- call statement() -%} {% set tmp_table_sql -%} - with dbt_incr_sbq as ( - {{ sql }} - ) - select * from dbt_incr_sbq + {# We are using a subselect instead of a CTE here to allow PostgreSQL to use indexes. -#} + select * from ({{ sql }}) as dbt_incr_sbq where ({{ sql_where }}) or ({{ sql_where }}) is null {%- endset %}