From 0f11202e13f7d222084bd6cc35a05d27c784e93f Mon Sep 17 00:00:00 2001 From: Wen Date: Mon, 25 Sep 2023 14:10:09 -0400 Subject: [PATCH] Avoid nested loop in query planner execution (#612) * Adjust not equals application order - Move relation name not equals from outer SELECT `where` clause to dependency CTE - Seems to more reliably push query planner to avoid a nested loop - Should preserve original intent, i.e. to exclude self-references from final result - Execution plan suggests this reduces the row estimate for the outer SELECT - Real-world testing on internal company cluster under conditions where nested loop is used for original, shows that the new approach continues to perform and behave as expected (no nested loop) * Add changie entry --- .changes/unreleased/Fixes-20230923-091155.yaml | 6 ++++++ dbt/include/redshift/macros/relations.sql | 2 +- 2 files changed, 7 insertions(+), 1 deletion(-) create mode 100644 .changes/unreleased/Fixes-20230923-091155.yaml diff --git a/.changes/unreleased/Fixes-20230923-091155.yaml b/.changes/unreleased/Fixes-20230923-091155.yaml new file mode 100644 index 000000000..055d4dc7e --- /dev/null +++ b/.changes/unreleased/Fixes-20230923-091155.yaml @@ -0,0 +1,6 @@ +kind: Fixes +body: avoid nested loop in query planner execution of redshift__get_relations +time: 2023-09-23T09:11:55.2152725-04:00 +custom: + Author: slin30 + Issue: "609" diff --git a/dbt/include/redshift/macros/relations.sql b/dbt/include/redshift/macros/relations.sql index 28c6bc377..6d83c36b9 100644 --- a/dbt/include/redshift/macros/relations.sql +++ b/dbt/include/redshift/macros/relations.sql @@ -24,6 +24,7 @@ with from pg_depend left join pg_rewrite on pg_depend.objid = pg_rewrite.oid + where coalesce(pg_rewrite.ev_class, pg_depend.objid) != pg_depend.refobjid ) select distinct @@ -36,7 +37,6 @@ join relation ref on dependency.ref_relation_id = ref.relation_id join relation dep on dependency.dep_relation_id = dep.relation_id -where ref.relation_name != dep.relation_name {%- endcall -%}