Skip to content

Commit cfad0ed

Browse files
Mohammad alkhassawnehMohammad alkhassawneh
authored andcommitted
Fix superfluous SELECT when scheduling/dispatching a single job
When a single job is enqueued, each Execution subclass fires a SELECT on solid_queue_jobs before INSERT. This happens because: 1. assumes_attributes_from_job sets a before_create callback that calls job.send(attribute) for each attribute to copy from the job record. 2. Since create_or_find_by!(job_id: id) only sets the job_id foreign key, the job association isn't loaded — so accessing it triggers a SELECT. The bulk path (enqueue_all) already avoids this by using insert_all, which bypasses callbacks entirely. Fix: pass job: self via the create_or_find_by! block so the association is already loaded in memory when both the before_create callback and the belongs_to validation run. No DB round-trip needed. Applied to all three single-job dispatch paths: schedule (ScheduledExecution), ready (ReadyExecution), and block (BlockedExecution).
1 parent 73602fe commit cfad0ed

4 files changed

Lines changed: 18 additions & 3 deletions

File tree

‎app/models/solid_queue/job/concurrency_controls.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def handle_concurrency_conflict
6363
end
6464

6565
def block
66-
BlockedExecution.create_or_find_by!(job_id: id)
66+
BlockedExecution.create_or_find_by!(job_id: id) { |e| e.job = self }
6767
end
6868

6969
def release_next_blocked_job

‎app/models/solid_queue/job/executable.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def discard
106106

107107
private
108108
def ready
109-
ReadyExecution.create_or_find_by!(job_id: id)
109+
ReadyExecution.create_or_find_by!(job_id: id) { |e| e.job = self }
110110
end
111111

112112
def execution

‎app/models/solid_queue/job/schedulable.rb‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def scheduled?
3838

3939
private
4040
def schedule
41-
ScheduledExecution.create_or_find_by!(job_id: id)
41+
ScheduledExecution.create_or_find_by!(job_id: id) { |e| e.job = self }
4242
end
4343

4444
def execution

‎test/models/solid_queue/job_test.rb‎

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,21 @@ class DiscardableNonOverlappingGroupedJob2 < NonOverlappingJob
8787
assert_equal solid_queue_job.scheduled_at, execution.scheduled_at
8888
end
8989

90+
test "scheduling a single future job does not SELECT the job record back" do
91+
active_job = AddToBufferJob.new(1)
92+
job_selects = 0
93+
94+
subscriber = ->(*, payload) do
95+
job_selects += 1 if payload[:sql].match?(/\ASELECT.*FROM.*solid_queue_jobs/i)
96+
end
97+
98+
ActiveSupport::Notifications.subscribed(subscriber, "sql.active_record") do
99+
SolidQueue::Job.enqueue(active_job, scheduled_at: 5.minutes.from_now)
100+
end
101+
102+
assert_equal 0, job_selects
103+
end
104+
90105
test "enqueue jobs within a connected_to block for the primary DB" do
91106
ShardedRecord.connected_to(role: :writing, shard: :shard_two) do
92107
ShardedJobResult.create!(value: "in shard two")

0 commit comments

Comments
 (0)