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.
load_async
will also fall back to executing in the foreground in the test environment when transactional fixtures are enabled.
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
696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 |
# File 'activerecord/lib/active_record/relation.rb', line 696 def load_async return load if !connection.async_enabled? unless loaded? result = exec_main_query(async: connection.current_transaction.closed?) if result.is_a?(Array) @records = result else @future_result = result end @loaded = true end self end |