Search code examples
rubysqliterhomobilerhodes

RhoMobile 13,000 inserts causing issues due to time


I have a problem (due to time) when inserting around 13,000 records into the devices database.

Is there any way to optimize this? Is it possible to put these all into one transaction (as I believe that it is currently creating one transaction per insert (which apparently has a diabolical effect on speed)).

Currently this takes around 10 minutes, this includes converting CSV to a hash (this doesn't seem to be the bottleneck).

Stupidly I am not using RhoSync...

Thanks


Solution

  • Set up a transaction around the inserts and then only commit at the end.

    From their FAQ.

    http://docs.rhomobile.com/faq#how-can-i-seed-a-large-amount-of-data-into-my-application-with-rhom

    db = ::Rho::RHO.get_src_db('Model')
    db.start_transaction
    begin
      items.each do |item|
        # create hash of attribute/value pairs
        data = {
          :field1 => item['value1'], 
          :field2 => item['value2'] 
        } 
        # Creates a new Model object and saves it
        new_item = Model.create(data)
      end
     db.commit
    rescue
     db.rollback
    end
    

    I've found this technique to be a tremendous speed up.