Skip to content

Commit 2c4bbd3

Browse files
committed
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
1 parent 0b13202 commit 2c4bbd3

2 files changed

Lines changed: 10 additions & 1 deletion

File tree

app/models/solid_queue/blocked_execution.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,9 @@ def release_many(concurrency_keys)
2626

2727
def release_one(concurrency_key)
2828
transaction do
29-
if execution = ordered.where(concurrency_key: concurrency_key).limit(1).non_blocking_lock.first
29+
if execution = ordered.where(concurrency_key: concurrency_key).limit(1)
30+
.use_index(:index_solid_queue_blocked_executions_for_release)
31+
.non_blocking_lock.first
3032
execution.release
3133
end
3234
end

app/models/solid_queue/record.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,13 @@ def supports_insert_conflict_target?
2020
connection.supports_insert_conflict_target?
2121
end
2222
end
23+
24+
# Pass index hints to the query optimizer using SQL comment hints.
25+
# Uses MySQL 8 optimizer hint query comments, which SQLite and
26+
# PostgreSQL ignore.
27+
def use_index(*indexes)
28+
optimizer_hints "INDEX(#{quoted_table_name} #{indexes.join(', ')})"
29+
end
2330
end
2431
end
2532
end

0 commit comments

Comments
 (0)