Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve performance of delete+insert incremental strategy with null equality check changes #834

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions dbt-adapters/.changes/unreleased/Fixes-20250221-092716.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: Fixes
body: Fix incremental delete+insert SQL
time: 2025-02-21T09:27:16.166423+02:00
custom:
Author: ataft, adrianburusdbt
Issue: 150, 364
Original file line number Diff line number Diff line change
Expand Up @@ -61,36 +61,23 @@
{%- set dest_cols_csv = get_quoted_csv(dest_columns | map(attribute="name")) -%}

{% if unique_key %}
{% if unique_key is sequence and unique_key is not string %}
delete from {{ target }}
using {{ source }}
where (
{% for key in unique_key %}
{% set source_unique_key = (source ~ "." ~ key) | trim %}
{% set target_unique_key = (target ~ "." ~ key) | trim %}
{{ equals(source_unique_key, target_unique_key) }}
{{ "and " if not loop.last}}
{% endfor %}
{% if incremental_predicates %}
{% for predicate in incremental_predicates %}
and {{ predicate }}
{% endfor %}
{% endif %}
);
{% else %}
delete from {{ target }}
where (
{{ unique_key }}) in (
select ({{ unique_key }})
from {{ source }}
)
{%- if incremental_predicates %}
{% for predicate in incremental_predicates %}
and {{ predicate }}
{% endfor %}
{%- endif -%};

{% if unique_key is string %}
{% set unique_key = [unique_key] %}
{% endif %}

{%- set unique_key_str = unique_key|join(', ') -%}

delete from {{ target }}
where ({{ unique_key_str }}) in (
select distinct {{ unique_key_str }}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the distinct here is unnecessary and would make it take longer since it needs to reduce dupes. I don't have proof of that, but I generally don't use distinct when using a where <x> in <y> clause.

from {{ source }}
)
{%- if incremental_predicates %}
{% for predicate in incremental_predicates %}
and {{ predicate }}
{% endfor %}
{%- endif -%};

{% endif %}

insert into {{ target }} ({{ dest_cols_csv }})
Expand Down
Loading