From 0ada4008ac4aba78fc491ceb9d720cde070d2b99 Mon Sep 17 00:00:00 2001 From: Martin Lue Date: Mon, 23 Jul 2018 16:30:50 -0400 Subject: [PATCH] Rewrite SQL for improved performance Replace WHERE foo NOT IN subquery with LEFT JOIN Use SELECT DISTINCT to avoid double counting if relationships test is applied on fields with non-unique values --- .../macros/schema_tests/relationships.sql | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) diff --git a/dbt/include/global_project/macros/schema_tests/relationships.sql b/dbt/include/global_project/macros/schema_tests/relationships.sql index 56977beb64a..7d7c08ef90f 100644 --- a/dbt/include/global_project/macros/schema_tests/relationships.sql +++ b/dbt/include/global_project/macros/schema_tests/relationships.sql @@ -1,17 +1,16 @@ {% macro test_relationships(model, field, to, from) %} - select count(*) from ( - select - {{ from }} as id - - from {{ model }} - where {{ from }} is not null - and {{ from }} not in (select {{ field }} - from {{ to }}) + select distinct + m.{{ from }} as id + from {{ model }} m + left join {{ to }} t on t.{{ field }} = m.{{ from }} + where + m.{{ from }} is not null and + t.{{ field }} is null ) validation_errors