Skip to content

OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub

increase performance of scheduling by avoiding paths aggregation

ulferts33081e1f97ef
diff --git a/app/models/work_packages/scopes/for_scheduling.rb b/app/models/work_packages/scopes/for_scheduling.rb
index 9723ba66e7e..bd860f679c9 100644
--- a/app/models/work_packages/scopes/for_scheduling.rb
+++ b/app/models/work_packages/scopes/for_scheduling.rb
@@ -116,7 +116,11 @@ module WorkPackages::Scopes
RECURSIVE
#{paths_sql(work_packages)}
- SELECT id FROM clean_paths WHERE NOT clean_paths.manually AND NOT clean_paths.descendants_manually
+ SELECT id
+ FROM clean_paths
+ WHERE
+ NOT clean_paths.manually
+ AND NOT clean_paths.descendants_manually
SQL
WorkPackage
@@ -171,13 +175,13 @@ module WorkPackages::Scopes
# * The current paths all end in manually scheduled work packages
# Both conditions can also stop the recursion together.
def paths_sql(work_packages)
- values = work_packages.map { |wp| "(#{wp.id},ARRAY[#{wp.id}], false, false)" }.join(', ')
+ values = work_packages.map { |wp| "(#{wp.id}, false, false)" }.join(', ')
<<~SQL
- clean_paths (id, path, manually, descendants_manually) AS (
- SELECT * FROM (VALUES#{values}) AS t(id, path, manually, descendants_manually)
+ clean_paths (id, manually, descendants_manually) AS (
+ SELECT * FROM (VALUES#{values}) AS t(id, manually, descendants_manually)
- UNION ALL
+ UNION
SELECT
CASE
@@ -185,11 +189,6 @@ module WorkPackages::Scopes
THEN relations.from_id
ELSE relations.to_id
END id,
- CASE
- WHEN relations.to_id = clean_paths.id
- THEN array_append(path, relations.from_id)
- ELSE array_append(path, relations.to_id)
- END path,
work_packages.schedule_manually manually,
COALESCE(descendants.schedule_manually, false) descendants_manually
FROM
@@ -199,34 +198,26 @@ module WorkPackages::Scopes
ON NOT clean_paths.manually AND NOT clean_paths.descendants_manually
AND (#{relations_condition_sql})
AND
- ((relations.to_id = clean_paths.id AND NOT relations.from_id = any(clean_paths.path))
- OR (relations.from_id = clean_paths.id AND NOT relations.to_id = any(clean_paths.path) AND relations.follows = 0))
+ ((relations.to_id = clean_paths.id)
+ OR (relations.from_id = clean_paths.id AND relations.follows = 0))
LEFT JOIN work_packages
ON (CASE
WHEN relations.to_id = clean_paths.id
THEN relations.from_id
ELSE relations.to_id
END) = work_packages.id
- LEFT JOIN relations hierarchy_relations
-ON work_packages.id = hierarchy_relations.from_id AND (hierarchy_relations.follows = 0 AND "hierarchy_relations"."relates" = 0 AND "hierarchy_relations"."duplicates" = 0 AND "hierarchy_relations"."blocks" = 0 AND "hierarchy_relations"."includes" = 0 AND "hierarchy_relations"."requires" = 0
-AND (hierarchy_relations.hierarchy + hierarchy_relations.relates + hierarchy_relations.duplicates + hierarchy_relations.follows + hierarchy_relations.blocks + hierarchy_relations.includes + hierarchy_relations.requires = 1))
-LEFT JOIN work_packages descendants
-ON hierarchy_relations.to_id = descendants.id
-/*
-GROUP BY (CASE
-WHEN relations.to_id = clean_paths.id
-THEN relations.from_id
-ELSE relations.to_id
-END,
-
-CASE
- WHEN relations.to_id = clean_paths.id
- THEN array_append(path, relations.from_id)
- ELSE array_append(path, relations.to_id)
- END
-)
- */
-
+ LEFT JOIN (
+ SELECT
+ relations.from_id,
+ bool_and(COALESCE(work_packages.schedule_manually, false)) schedule_manually
+ FROM relations relations
+ JOIN work_packages
+ ON
+ work_packages.id = relations.to_id
+ AND (relations.follows = 0 AND "relations"."relates" = 0 AND "relations"."duplicates" = 0 AND "relations"."blocks" = 0 AND "relations"."includes" = 0 AND "relations"."requires" = 0
+ AND (relations.hierarchy + relations.relates + relations.duplicates + relations.follows + relations.blocks + relations.includes + relations.requires = 1))
+ GROUP BY relations.from_id
+ ) descendants ON work_packages.id = descendants.from_id
)
SQL
end
diff --git a/spec/models/work_packages/scopes/for_scheduling_spec.rb b/spec/models/work_packages/scopes/for_scheduling_spec.rb
index f4d19641df8..1981b228f4d 100644
--- a/spec/models/work_packages/scopes/for_scheduling_spec.rb
+++ b/spec/models/work_packages/scopes/for_scheduling_spec.rb
@@ -295,7 +295,7 @@ describe WorkPackages::Scopes::ForScheduling, 'allowed scope' do
let!(:existing_work_packages) { [successor, successor_child, successor_child2, successor_successor] }
context 'with all scheduled automatically' do
- it 'consists of the successor, its child and parent' do
+ it 'consists of the successor, its child and the successor˚s successor' do
expect(described_class.fetch([origin]))
.to match_array([successor, successor_child, successor_child2, successor_successor])
end
@@ -306,7 +306,7 @@ describe WorkPackages::Scopes::ForScheduling, 'allowed scope' do
successor_child2.update_column(:schedule_manually, true)
end
- it 'is empty' do
+ it 'consists of the successor, its automatically scheduled child and the successor˚s successor' do
expect(described_class.fetch([origin]))
.to match_array([successor_child, successor, successor_successor])
end
@@ -391,7 +391,7 @@ describe WorkPackages::Scopes::ForScheduling, 'allowed scope' do
let!(:existing_work_packages) { [successor, successor_successor] }
context 'with all scheduled automatically' do
- it 'consists of the both successors' do
+ it 'consists of both successors' do
expect(described_class.fetch([origin]))
.to match_array([successor, successor_successor])
end