improve performance of relation query

By restructuring the `or` to be within the subselect, the query engine is able to filter down on the relations way more efficient than before.

As this query is used within WP scheduling, changes to the dates are more performant now. On my setup with about 500k relations on MySql, the change saved about 500ms
pull/6258/head
Jens Ulferts 7 years ago
parent d9932fe262
commit 5799105e03
No known key found for this signature in database
GPG Key ID: 3CAA4B1182CF5308
  1. 8
      app/models/relation.rb

@ -144,8 +144,12 @@ class Relation < ActiveRecord::Base
end
def self.from_work_package_or_ancestors(work_package)
where(from_id: work_package.ancestors_relations.select(:from_id))
.or(where(from_id: work_package.id))
ancestor_or_self_ids = work_package
.ancestors_relations
.or(where(from_id: work_package.id))
.select(:from_id)
where(from_id: ancestor_or_self_ids)
end
def self.from_parent_to_self_and_descendants(work_package)

Loading…
Cancel
Save