Is it possible to transparently implement the Future template for ActiveRecord queries in Ruby 1.9?

I am working on an existing Rails 2 site with a large code base recently updated to Ruby 1.9.2 and gem mysql2. I noticed that this setting allows you to non-block database queries; you can do client.query(sql, :async => true) and then call client.async_result , which blocks until the request completes.

It seems to me that we can get a performance boost due to all ActiveRecord queries that return a collection decline for locking until a method is called in the collection. eg.

 @widgets = Widget.find(:all, :conditions=> conditions) #sends the query do_some_stuff_that_doesn't_require_widgets @widgets.each do #if the query hasn't completed yet, wait until it does, then populate @widgets with the result. Iterate through @widgets ... 

This can be done using monkey-patching Base::find and related methods to create a new database client, send the request asynchronously, and then immediately return a delegate or other proxy object, which will be when any method is called on it , call client.async_result , create the result using ActiveRecord and delegate the method to this. ActiveRecord Associative objects already work similarly to implement ORM.

I can't find anyone to do this, and it seems like this is not an option in any version of Rails. I tried to implement it myself, and it works in the console (as long as I add ; 1 to the line that calls everything so that to_s not called as a result). But it seems that he is faced with all sorts of other magic and creates various problems.

So, is this a bad idea for some reason that I haven't thought about? If not, why is this not how ActiveRecord works? Is there a clean way to do this?

+4
source share
1 answer

I suspect that this .async_result method is not available for all database drivers; if not, this is not something that could be combined into common ActiveRecord calls.

A more portable way to improve performance when cycling over a large set of records would be to use find_each or find_in_batches. I think that they will work in rails 2.3, as well as in rails 3.x. http://guides.rubyonrails.org/active_record_querying.html#retrieving-multiple-objects-in-batches

+1
source

Source: https://habr.com/ru/post/1439860/


All Articles