From 01dbd70ba3959fab049d83fee93b93ceeb2ec013 Mon Sep 17 00:00:00 2001 From: Michelle Ark Date: Wed, 6 Nov 2024 17:33:12 -0500 Subject: [PATCH] [Microbatch] Optimizations: use view for temp relation + remove `using` clause during delete statement (#1192) --- .changes/unreleased/Fixes-20241104-104610.yaml | 7 +++++++ .../snowflake/macros/materializations/incremental.sql | 8 ++++---- dbt/include/snowflake/macros/materializations/merge.sql | 1 - 3 files changed, 11 insertions(+), 5 deletions(-) create mode 100644 .changes/unreleased/Fixes-20241104-104610.yaml diff --git a/.changes/unreleased/Fixes-20241104-104610.yaml b/.changes/unreleased/Fixes-20241104-104610.yaml new file mode 100644 index 000000000..c512d0bdd --- /dev/null +++ b/.changes/unreleased/Fixes-20241104-104610.yaml @@ -0,0 +1,7 @@ +kind: Fixes +body: 'Performance fixes for snowflake microbatch strategy: use temp view instead + of table, remove unnecessary ''using'' clause' +time: 2024-11-04T10:46:10.005317-05:00 +custom: + Author: michelleark + Issue: "1228" diff --git a/dbt/include/snowflake/macros/materializations/incremental.sql b/dbt/include/snowflake/macros/materializations/incremental.sql index d73525d6d..dbb79de02 100644 --- a/dbt/include/snowflake/macros/materializations/incremental.sql +++ b/dbt/include/snowflake/macros/materializations/incremental.sql @@ -20,7 +20,7 @@ The append strategy can use a view because it will run a single INSERT statement. - When unique_key is none, the delete+insert strategy can use a view beacuse a + When unique_key is none, the delete+insert and microbatch strategies can use a view beacuse a single INSERT statement is run with no DELETES as part of the statement. Otherwise, play it safe by using a temporary table. #} */ @@ -32,10 +32,10 @@ ) %} {% endif %} - {% if strategy == "delete+insert" and tmp_relation_type is not none and tmp_relation_type != "table" and unique_key is not none %} + {% if strategy in ["delete+insert", "microbatch"] and tmp_relation_type is not none and tmp_relation_type != "table" and unique_key is not none %} {% do exceptions.raise_compiler_error( "In order to maintain consistent results when `unique_key` is not none, - the `delete+insert` strategy only supports `table` for `tmp_relation_type` but " + the `" ~ strategy ~ "` strategy only supports `table` for `tmp_relation_type` but " ~ tmp_relation_type ~ " was specified." ) %} @@ -49,7 +49,7 @@ {{ return("view") }} {% elif strategy in ("default", "merge", "append") %} {{ return("view") }} - {% elif strategy == "delete+insert" and unique_key is none %} + {% elif strategy in ["delete+insert", "microbatch"] and unique_key is none %} {{ return("view") }} {% else %} {{ return("table") }} diff --git a/dbt/include/snowflake/macros/materializations/merge.sql b/dbt/include/snowflake/macros/materializations/merge.sql index 57c58afdd..c8ac8d6fd 100644 --- a/dbt/include/snowflake/macros/materializations/merge.sql +++ b/dbt/include/snowflake/macros/materializations/merge.sql @@ -66,7 +66,6 @@ {% do arg_dict.update({'incremental_predicates': incremental_predicates}) %} delete from {{ target }} DBT_INTERNAL_TARGET - using {{ source }} where ( {% for predicate in incremental_predicates %} {%- if not loop.first %}and {% endif -%} {{ predicate }}