From cdcd437e738eba0770778e6e5387269c6d946647 Mon Sep 17 00:00:00 2001 From: Michael Fowler Date: Thu, 5 Jan 2023 14:58:06 +1300 Subject: [PATCH] Avoid including unnecessary pg_locks references when counting jobs Previously, the executed query for in particular the `succeeded` scope included a join against `pg_locks`. This is not particularly useful, and unfortunately triggers evaluation of that join criterion, with noticable expense as job volume grows. This change modifies the jobs filter logic to only include those joins in the necessary locations (`running`) and when loading records for practical enumeration. --- app/filters/good_job/base_filter.rb | 6 +++++- app/filters/good_job/jobs_filter.rb | 8 ++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/app/filters/good_job/base_filter.rb b/app/filters/good_job/base_filter.rb index 84d4bc0b7..090d4d7dd 100644 --- a/app/filters/good_job/base_filter.rb +++ b/app/filters/good_job/base_filter.rb @@ -14,7 +14,7 @@ def initialize(params, base_query = nil) def records after_scheduled_at = params[:after_scheduled_at].present? ? Time.zone.parse(params[:after_scheduled_at]) : nil - filtered_query.display_all( + query_for_records.display_all( after_scheduled_at: after_scheduled_at, after_id: params[:after_id] ).limit(params.fetch(:limit, DEFAULT_LIMIT)) @@ -62,6 +62,10 @@ def filtered_count private + def query_for_records + raise NotImplementedError + end + def default_base_query raise NotImplementedError end diff --git a/app/filters/good_job/jobs_filter.rb b/app/filters/good_job/jobs_filter.rb index c84472302..d8985943a 100644 --- a/app/filters/good_job/jobs_filter.rb +++ b/app/filters/good_job/jobs_filter.rb @@ -2,7 +2,7 @@ module GoodJob class JobsFilter < BaseFilter def states - query = filtered_query(params.except(:state)).unscope(:select) + query = filtered_query(params.except(:state)) { 'scheduled' => query.scheduled.count, 'retried' => query.retried.count, @@ -14,7 +14,7 @@ def states end def filtered_query(filter_params = params) - query = base_query.includes(:executions).includes_advisory_locks + query = base_query query = query.job_class(filter_params[:job_class]) if filter_params[:job_class].present? query = query.where(queue_name: filter_params[:queue_name]) if filter_params[:queue_name].present? @@ -47,6 +47,10 @@ def filtered_count private + def query_for_records + filtered_query.includes(:executions).includes_advisory_locks + end + def default_base_query GoodJob::Job.all end