Method: ActiveRecord::Relation#load_async
- Defined in:
- activerecord/lib/active_record/relation.rb
#load_async ⇒ Object
Schedule the query to be performed from a background thread pool.
Post.where(published: true).load_async # => #<ActiveRecord::Relation>
When the Relation
is iterated, if the background query wasn’t executed yet, it will be performed by the foreground thread.
Note that config.active_record.async_query_executor must be configured for queries to actually be executed concurrently. Otherwise it defaults to executing them in the foreground.
If the query was actually executed in the background, the Active Record logs will show it by prefixing the log line with ASYNC
:
ASYNC Post Load (0.0ms) (db time 2ms) SELECT "posts".* FROM "posts" LIMIT 100
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 |
# File 'activerecord/lib/active_record/relation.rb', line 1138 def load_async with_connection do |c| return load if !c.async_enabled? unless loaded? result = exec_main_query(async: !c.current_transaction.joinable?) if result.is_a?(Array) @records = result else @future_result = result end @loaded = true end end self end |