diff --git a/.changes/unreleased/Under the Hood-20241205-143144.yaml b/.changes/unreleased/Under the Hood-20241205-143144.yaml new file mode 100644 index 00000000000..10aa90a2b2a --- /dev/null +++ b/.changes/unreleased/Under the Hood-20241205-143144.yaml @@ -0,0 +1,7 @@ +kind: Under the Hood +body: Improve selection peformance by optimizing the select_children() and select_parents() + functions. +time: 2024-12-05T14:31:44.584216-05:00 +custom: + Author: peterallenwebb + Issue: "11099" diff --git a/core/dbt/graph/graph.py b/core/dbt/graph/graph.py index cf569f3547d..4d1e545cbd2 100644 --- a/core/dbt/graph/graph.py +++ b/core/dbt/graph/graph.py @@ -67,8 +67,14 @@ def select_children( while len(selected) > 0 and (max_depth is None or i < max_depth): next_layer: Set[UniqueId] = set() for node in selected: - next_layer.update(self.descendants(node, 1)) - next_layer = next_layer - children # Avoid re-searching + next_layer.update( + iter( + e[1] + for e in self.graph.out_edges(node) + if e[1] not in children + and self.filter_edges_by_type(e[0], e[1], "parent_test") + ) + ) children.update(next_layer) selected = next_layer i += 1 @@ -86,8 +92,14 @@ def select_parents( while len(selected) > 0 and (max_depth is None or i < max_depth): next_layer: Set[UniqueId] = set() for node in selected: - next_layer.update(self.ancestors(node, 1)) - next_layer = next_layer - parents # Avoid re-searching + next_layer.update( + iter( + e[0] + for e in self.graph.in_edges(node) + if e[0] not in parents + and self.filter_edges_by_type(e[0], e[1], "parent_test") + ) + ) parents.update(next_layer) selected = next_layer i += 1