Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Speeding up XML to MySQL with Nokogiri in Rails

I'm writing large amounts of data from XML feeds to my MySQL database in my Rails 3 app using Nokogiri. Everything is working fine but it's slower than I would like.

Is there any way to speed up the process? This is simplified version of the script I'm using:

url = "http://example.com/urltoxml"
doc = Nokogiri::XML(open(url))
doc.xpath("//item").each do |record|

  guid = record.xpath("id").inner_text
  price = record.xpath("price").inner_text
  shipping = record.xpath("shipping").inner_text

  data = Table.new(
    :guid => guid,
    :price => price,
    :shipping => shipping
  )
  if price != ""
    data.save
  end

end

Thnx in advance

like image 637
Reinier Avatar asked Jan 31 '26 09:01

Reinier


1 Answers

I guess your problem is not from parsing XML, but is that you insert the records one by one in the DB, which is very costly.

Unfortunately, AFAIK Rails does not provide a native way to mass-insert records. There once was a gem that did it, but I can't get my hand back on it.

"Mass inserting data in Rails without killing your performance", though, provides helpful insights on how to do it manually.

If you go this way, don't forget to process your nodes in batches if you don't want to end with a single 999-bazillion-rows INSERT statement.

like image 145
m_x Avatar answered Feb 02 '26 23:02

m_x



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!