Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delayed Job Batch Inserts

I'm in the process of optimizing my SQL queries on my heroku server so I can speed things up on one particular request. Right now I'm mainly looking at condensing all the INSERT queries into the fewest queries as possible.

At some point in my code I have this:

  jobs.each do |j|
    Delayed::Job.enqueue j
  end

I found out that every iteration sends a BEGIN, INSERT, COMMIT to the db. That jobs array can have from a few to a couple hundred objects in it. I have looked for a way to batch insert delayed jobs but couldn't find anything. Any idea of how to achieve that?

like image 254
samvermette Avatar asked Nov 05 '22 03:11

samvermette


2 Answers

I've been using AR-Extensions for a long time to insert bulk data from models into the database.

That was on Rails 2.3.x though, be careful that there are now different versions depending on the Rails version: http://www.continuousthinking.com/tags/arext

I'm not sure how Delayed::Job works, but guessing from your example, I'd assume it inserts a record per job into a table which then serves as the queue. You could extend that, using AR-Extensions, to collect all those models and insert all jobs at once.

like image 104
Martin T. Avatar answered Nov 09 '22 04:11

Martin T.


I ended up enqueuing my User object instead, which had a jobs attribute. So 1 insert instead of jobs.length inserts.

like image 37
samvermette Avatar answered Nov 09 '22 02:11

samvermette