speedup sql for alertable work packages

* avoid calculation on dates within the SQL so that indices on the dates can be employed
* add indices for start and due date
pull/11880/head
ulferts 2 years ago
parent fb78d2861a
commit 55d1ccc121
No known key found for this signature in database
GPG Key ID: A205708DE1284017
  1. 15
      app/workers/notifications/create_date_alerts_notifications_job/alertable_work_packages.rb
  2. 6
      db/migrate/20230105134940_work_package_date_indices.rb

@ -55,7 +55,6 @@ class Notifications::CreateDateAlertsNotificationsJob::AlertableWorkPackages
def query
today = Arel::Nodes::build_quoted(Date.current).to_sql
alertable_durations = Arel::Nodes::Grouping.new(UserPreferences::ParamsContract::DATE_ALERT_DURATIONS.compact).to_sql
alertables = alertable_work_packages
.select(:id,
@ -63,9 +62,9 @@ class Notifications::CreateDateAlertsNotificationsJob::AlertableWorkPackages
"work_packages.start_date - #{today} AS start_delta",
"work_packages.due_date - #{today} AS due_delta",
"#{today} - work_packages.due_date AS overdue_delta")
.where("work_packages.start_date - #{today} IN #{alertable_durations} " \
"OR work_packages.due_date - #{today} IN #{alertable_durations} " \
"OR #{today} - work_packages.due_date > 0")
.where("work_packages.start_date IN #{alertable_dates} " \
"OR work_packages.due_date IN #{alertable_dates} " \
"OR work_packages.due_date < #{today}")
<<~SQL.squish
WITH
@ -125,4 +124,12 @@ class Notifications::CreateDateAlertsNotificationsJob::AlertableWorkPackages
join_dependency = work_packages.construct_join_dependency([:status], Arel::Nodes::OuterJoin)
work_packages.joins(join_dependency)
end
def alertable_dates
dates = UserPreferences::ParamsContract::DATE_ALERT_DURATIONS
.compact
.map { |offset| Arel::Nodes::build_quoted(Date.current + offset.days) }
Arel::Nodes::Grouping.new(dates).to_sql
end
end

@ -0,0 +1,6 @@
class WorkPackageDateIndices < ActiveRecord::Migration[7.0]
def change
add_index :work_packages, :start_date
add_index :work_packages, :due_date
end
end
Loading…
Cancel
Save