From 7479a20b8d606b7a91a5f72c892eec66c85e214b Mon Sep 17 00:00:00 2001 From: Rosa Gutierrez Date: Sun, 22 Feb 2026 11:12:30 +0100 Subject: [PATCH] Add index hint for releasing blocked executions Use Rails' `optimizer_hints` to hint MySQL to use the `index_solid_queue_blocked_executions_for_release` composite index when releasing blocked executions, avoiding query planner misses and unnecessary row locks. The hint is rendered as a `/*+ ... */` SQL comment, which MySQL's optimizer reads and SQLite and PostgreSQL ignore as a regular comment. Fixes #694 --- app/models/solid_queue/blocked_execution.rb | 4 +++- app/models/solid_queue/record.rb | 7 +++++++ 2 files changed, 10 insertions(+), 1 deletion(-) diff --git a/app/models/solid_queue/blocked_execution.rb b/app/models/solid_queue/blocked_execution.rb index 4ad4d239..68551a5f 100644 --- a/app/models/solid_queue/blocked_execution.rb +++ b/app/models/solid_queue/blocked_execution.rb @@ -26,7 +26,9 @@ def release_many(concurrency_keys) def release_one(concurrency_key) transaction do - if execution = ordered.where(concurrency_key: concurrency_key).limit(1).non_blocking_lock.first + if execution = ordered.where(concurrency_key: concurrency_key).limit(1) + .use_index(:index_solid_queue_blocked_executions_for_release) + .non_blocking_lock.first execution.release end end diff --git a/app/models/solid_queue/record.rb b/app/models/solid_queue/record.rb index 0a704d2c..8c7000bf 100644 --- a/app/models/solid_queue/record.rb +++ b/app/models/solid_queue/record.rb @@ -20,6 +20,13 @@ def supports_insert_conflict_target? connection.supports_insert_conflict_target? end end + + # Pass index hints to the query optimizer using SQL comment hints. + # Uses MySQL 8 optimizer hint query comments, which SQLite and + # PostgreSQL ignore. + def use_index(*indexes) + optimizer_hints "INDEX(#{quoted_table_name} #{indexes.join(', ')})" + end end end end