Merge pull request #6851 from steffzt/feature/optimize-hierarchy-sql

Feature/optimize hierarchy sql
pull/6852/head
ulferts 6 years ago committed by GitHub
commit 4c89814fcc
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 28
      app/models/relation/hierarchy_paths.rb

@ -93,16 +93,34 @@ module Relation::HierarchyPaths
SELECT SELECT
to_id, #{add_hierarchy_agg_function} AS path to_id, #{add_hierarchy_agg_function} AS path
FROM FROM
(SELECT to_id, from_id, hierarchy (SELECT to_id, from_id, hierarchy FROM relations #{add_conditions_and_union id}) as rel
FROM relations
WHERE hierarchy > 0 AND relates = 0 AND blocks = 0 AND duplicates = 0 AND includes = 0 AND requires = 0 AND follows = 0
#{add_hierarchy_id_constraint(id)}
) ordered_by_hierarchy
GROUP BY to_id GROUP BY to_id
#{add_hierarchy_conflict_statement} #{add_hierarchy_conflict_statement}
SQL SQL
end end
def self.add_conditions_and_union(id)
if id.nil?
<<-SQL
WHERE hierarchy > 0 AND relates = 0 AND blocks = 0 AND duplicates = 0 AND includes = 0 AND requires = 0 AND follows = 0
SQL
else
<<-SQL
WHERE to_id = #{id} AND
hierarchy > 0 AND relates = 0 AND blocks = 0 AND duplicates = 0 AND includes = 0 AND requires = 0 AND follows = 0
UNION SELECT to_id,from_id,hierarchy
FROM relations
WHERE from_id=#{id} AND
hierarchy > 0 AND relates = 0 AND blocks = 0 AND duplicates = 0 AND includes = 0 AND requires = 0 AND follows = 0
UNION SELECT b.to_id, b.from_id, b.hierarchy FROM relations a
JOIN relations b ON b.to_id = a.to_id
WHERE a.from_id = #{id} AND
a.hierarchy > 0 AND a.relates = 0 AND a.blocks = 0 AND a.duplicates = 0 AND a.includes = 0 AND a.requires = 0 AND a.follows = 0 AND
b.hierarchy > 0 AND b.relates = 0 AND b.blocks = 0 AND b.duplicates = 0 AND b.includes = 0 AND b.requires = 0 AND b.follows = 0
SQL
end
end
def self.remove_hierarchy_path_sql(id = nil) def self.remove_hierarchy_path_sql(id = nil)
id_constraint = if id id_constraint = if id
"WHERE work_package_id = #{id}" "WHERE work_package_id = #{id}"

Loading…
Cancel
Save