From 3ae32de4f8bef66734a574506b85e8be262bc4e6 Mon Sep 17 00:00:00 2001 From: Drew Banin Date: Tue, 20 Sep 2016 16:12:06 -0400 Subject: [PATCH] rework incremental models to use a temp table this should speed up query times and also provide a more correct implementation. See: https://github.com/analyst-collective/dbt/issues/138 --- dbt/templates.py | 40 +++++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 19 deletions(-) diff --git a/dbt/templates.py b/dbt/templates.py index 4b29458c1db..329a6197049 100644 --- a/dbt/templates.py +++ b/dbt/templates.py @@ -9,32 +9,32 @@ class BaseCreateTemplate(object): # but you cannot explicitly set them in the CREATE TABLE ... LIKE statement. # via http://docs.aws.amazon.com/redshift/latest/dg/r_CREATE_TABLE_NEW.html incremental_template = """ -create temporary table "{identifier}__dbt_incremental_tmp" {dist_qualifier} {sort_qualifier} as ( +create temporary table "{identifier}__dbt_incremental_empty_tmp" {dist_qualifier} {sort_qualifier} as ( select * from ( {query} ) as tmp limit 0 ); -create table if not exists "{schema}"."{identifier}" (like "{identifier}__dbt_incremental_tmp"); +create table if not exists "{schema}"."{identifier}" (like "{identifier}__dbt_incremental_empty_tmp"); -{incremental_delete_statement} - -insert into "{schema}"."{identifier}" ( +create temporary table "{identifier}__dbt_incremental_tmp" as ( with dbt_incr_sbq as ( {query} ) select * from dbt_incr_sbq where ({sql_where}) or ({sql_where}) is null ); + +{incremental_delete_statement} + +insert into "{schema}"."{identifier}" ( + select * from "{identifier}__dbt_incremental_tmp" +); """ incremental_delete_template = """ delete from "{schema}"."{identifier}" where ({unique_key}) in ( - with dbt_delete_sbq as ( - {query} - ) - select ({unique_key}) from dbt_delete_sbq - where ({sql_where}) or ({sql_where}) is null + select ({unique_key}) from "{identifier}__dbt_incremental_tmp" ); """ @@ -105,17 +105,16 @@ class DryCreateTemplate(object): incremental_template = """ -create temporary table "{identifier}__dbt_incremental_tmp" {dist_qualifier} {sort_qualifier} as ( +create temporary table "{identifier}__dbt_incremental_empty_tmp" {dist_qualifier} {sort_qualifier} as ( select * from ( {query} ) as tmp limit 0 ); -create table if not exists "{schema}"."{identifier}" (like "{identifier}__dbt_incremental_tmp"); -{incremental_delete_statement} +create table if not exists "{schema}"."{identifier}" (like "{identifier}__dbt_incremental_empty_tmp"); -insert into "{schema}"."{identifier}" ( +create temporary table "{identifier}__dbt_incremental_tmp" as ( with dbt_incr_sbq as ( {query} ) @@ -123,15 +122,18 @@ class DryCreateTemplate(object): where ({sql_where}) or ({sql_where}) is null limit 0 ); + + +{incremental_delete_statement} + +insert into "{schema}"."{identifier}" ( + select * from "{identifier}__dbt_incremental_tmp" +); """ incremental_delete_template = """ delete from "{schema}"."{identifier}" where ({unique_key}) in ( - with dbt_delete_sbq as ( - {query} - ) - select ({unique_key}) from dbt_delete_sbq - where ({sql_where}) or ({sql_where}) is null + select ({unique_key}) from "{identifier}__dbt_incremental_tmp" ); """