Query scheduling is a feature of Rails that allows you to control the order in which database queries are executed.
By default, all queries are processed in one thread, but you can use query scheduling to place them in multiple threads or even assign them to a particular processor.
Hence, asynchronous querying reduces the query time significantly as opposed to running multiple queries serially. While, this may not be a significant issue with smaller applications that handle a small amount of data, however, as the database grows in size the response time for these queries also grows.
Take a look at the before:
1
2
@studios = Studio.all
@artists = Artist.order(published_at: :desc)
This is what it is now:
Rails 7 introduces the load_async
method that schedules queries to be performed asynchronously from a thread pool. For controllers that require to perform multiple independent queries, this method works great.
1
2
@studios = Studio.all.load_async
@artists = Artist.order(published_at: :desc).load_async
The above queries are performed in a thread pool, while the rest of the work is still done on the main thread.
As a result, you can optimize performance by running database queries when the database is not busy, which can lead to better results and faster response times.
In addition, query scheduling can help prevent memory leaks by ensuring that database connections are closed after an application terminates.
Another good example of this is if you have a page with a form that submits data to the server, but the page does not display until all of this data has been submitted. In this case, it’s crucial that only one query is run at a time. If multiple queries were run simultaneously, it would slow down the website significantly.
For more changes and discussions on the change above, refer here