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

Improve performance of querying with timestamps

Christophe Bliardb44de2581d65
diff --git a/app/models/journable/historic_active_record_relation.rb b/app/models/journable/historic_active_record_relation.rb
index c709950d3f9..fa2411e7f18 100644
--- a/app/models/journable/historic_active_record_relation.rb
+++ b/app/models/journable/historic_active_record_relation.rb
@@ -108,14 +108,13 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
relation = substitute_database_table_in_where_clause(relation)
relation = add_timestamp_condition(relation)
relation = add_join_on_journables_table_with_created_at_column(relation)
+ relation = add_join_projects_on_journables(relation)
relation = select_columns_from_the_appropriate_tables(relation)
# Based on the previous modifications, build the algebra object.
- @arel = relation.call_original_build_arel(aliases)
- @arel = modify_order_clauses(@arel)
- @arel = modify_joins(@arel)
-
- @arel
+ arel = relation.call_original_build_arel(aliases)
+ arel = modify_order_clauses(arel)
+ modify_joins(arel)
end
def call_original_build_arel(aliases = nil)
@@ -221,10 +220,32 @@ class Journable::HistoricActiveRecordRelation < ActiveRecord::Relation
#
def add_join_on_journables_table_with_created_at_column(relation)
relation \
- .joins("INNER JOIN (SELECT id, created_at FROM \"#{model.table_name}\") AS journables " \
+ .joins("INNER JOIN (SELECT id, created_at#{', project_id' if include_projects?(relation)} " \
+ "FROM \"#{model.table_name}\") AS journables " \
"ON \"journables\".\"id\" = \"journals\".\"journable_id\"")
end
+ # Join the projects table on journables if :project is in the includes.
+ # It is needed when projects are filtered by id, and has to be done manually
+ # as eager_loading is disabled.
+ # Does not work yet for other includes.
+ #
+ def add_join_projects_on_journables(relation)
+ if include_projects?(relation)
+ relation
+ .except(:includes, :eager_load, :preload)
+ .joins('LEFT OUTER JOIN "projects" ' \
+ 'ON "projects"."id" = "journables"."project_id"')
+ else
+ relation
+ end
+ end
+
+ def include_projects?(relation)
+ include_values = relation.values.fetch(:includes, [])
+ include_values.include?(:project)
+ end
+
# Gather the columns we need in our model from the different tables in the sql query:
#
# - the `work_packages` table (journables)
diff --git a/spec/models/journable/historic_active_record_relation_spec.rb b/spec/models/journable/historic_active_record_relation_spec.rb
index b4c4761ff3b..8f800463528 100644
--- a/spec/models/journable/historic_active_record_relation_spec.rb
+++ b/spec/models/journable/historic_active_record_relation_spec.rb
@@ -431,6 +431,27 @@ describe Journable::HistoricActiveRecordRelation do
expect(subject).to include work_package
end
end
+
+ describe "using an initial scope with a include(:project)" do
+ # This is used in a work package query with timestamps.
+ let(:relation) do
+ WorkPackage
+ .includes(:project)
+ .where(projects: { id: project.id })
+ end
+
+ it "joins the projects table" do
+ sql = subject.to_sql.tr('"', '')
+ expect(sql).to include \
+ "LEFT OUTER JOIN projects ON projects.id = journables.project_id"
+ expect(sql).to include \
+ "WHERE projects.id = #{project.id}"
+ end
+
+ it "returns the requested work package" do
+ expect(subject).to include work_package
+ end
+ end
end
describe "#first" do