find_in_batches
does not allow pure SQL (so far as I can see).find_by_sql
does not have batch support (so far as I can see).
So how can I do something like find_in_batches_by_sql
?
The SQL is nasty programatically generated stuff and it points to 3rd party databases and the result sets can have hundred-thousands to millions of records returned.
Are there other cursor tricks with ActiveRecord I should look into?
Thanks.
Rails Active Records provide an interface and binding between the tables in a relational database and the Ruby program code that manipulates database records. Ruby method names are automatically generated from the field names of database tables.
Rails provides an ActiveRecord method called :includes which loads associated records in advance and limits the number of SQL queries made to the database. This technique is known as "eager loading" and in many cases will improve performance by a significant amount.
Updated to use the correct order for LIMIT and OFFSET
You could always break the SQL into parts and do something like
Model.select("*").where("WHERE CLAUSE HERE").joins("JOIN CLAUSES HERE").find_in_batches {...}
Or if you needed really fun SQL stuff you could just use an offset and limit and loop until you exhausted the results. Here's the basic idea:
offset = 0
limit = 1000
while(results)
results = Model.find_by_sql("<your SQL here> LIMIT #{limit} OFFSET #{offset}")
offset += limit
# Do stuff here
end
Note offset should after limit
offset = 0
limit = 1000
while(results)
results = Model.find_by_sql("<your SQL here> LIMIT #{limit} OFFSET #{offset}")
offset += limit
# Do stuff here
end
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With