Commit d3acc8cf authored by Imre Farkas's avatar Imre Farkas

Linear traversal query for Namespace#descendants

Linear queries have better performance than recursive CTEs

Changelog: added
parent a7111b39
...@@ -63,6 +63,12 @@ module Namespaces ...@@ -63,6 +63,12 @@ module Namespaces
lineage(top: self) lineage(top: self)
end end
def descendants
return super unless use_traversal_ids?
self_and_descendants.where.not(id: id)
end
def ancestors(hierarchy_order: nil) def ancestors(hierarchy_order: nil)
return super() unless use_traversal_ids? return super() unless use_traversal_ids?
return super() unless Feature.enabled?(:use_traversal_ids_for_ancestors, root_ancestor, default_enabled: :yaml) return super() unless Feature.enabled?(:use_traversal_ids_for_ancestors, root_ancestor, default_enabled: :yaml)
......
---
title: Linear traversal query for Namespace#descendants
merge_request: 59632
author:
type: performance
...@@ -441,6 +441,10 @@ RSpec.describe Group do ...@@ -441,6 +441,10 @@ RSpec.describe Group do
it { expect(group.self_and_descendants.to_sql).not_to include 'traversal_ids @>' } it { expect(group.self_and_descendants.to_sql).not_to include 'traversal_ids @>' }
end end
describe '#descendants' do
it { expect(group.descendants.to_sql).not_to include 'traversal_ids @>' }
end
describe '#ancestors' do describe '#ancestors' do
it { expect(group.ancestors.to_sql).not_to include 'traversal_ids <@' } it { expect(group.ancestors.to_sql).not_to include 'traversal_ids <@' }
end end
...@@ -453,6 +457,10 @@ RSpec.describe Group do ...@@ -453,6 +457,10 @@ RSpec.describe Group do
it { expect(group.self_and_descendants.to_sql).to include 'traversal_ids @>' } it { expect(group.self_and_descendants.to_sql).to include 'traversal_ids @>' }
end end
describe '#descendants' do
it { expect(group.descendants.to_sql).to include 'traversal_ids @>' }
end
describe '#ancestors' do describe '#ancestors' do
it { expect(group.ancestors.to_sql).to include "\"namespaces\".\"id\" = #{group.parent_id}" } it { expect(group.ancestors.to_sql).to include "\"namespaces\".\"id\" = #{group.parent_id}" }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment