# How should I do update?

**URL:** <https://www.ruby-forum.com/t/how-should-i-do-update/104755>\
**Category:** Rails\
**Created:** [May 22, 2007, 5:17am UTC](https://www.ruby-forum.com/t/how-should-i-do-update/104755 "2007-05-22T05:17:51Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [May 22, 2007, 5:17am UTC](https://www.ruby-forum.com/t/how-should-i-do-update/104755/1 "2007-05-22T05:17:51Z")

</div>

I have a record called a PMR. Each PMR has a number of Signatures.  
Each Signature has a number of Lines.

Currently I have a routine that is feed an entire PMR as a file and it  
parses it into the proper structure and stores it.

The problem is that the PMR gross over time. I am receiving it from  
another application. The easiest way to get it is to just get the  
whole thing.

I can re-parse the whole thing and create the internal Ruby  
structures. That process is pretty fast. But then what? Should I  
fetch the old and compare record by record and update only what  
changes? Or I could delete the old and save the new.

Any suggestions?

Thanks.

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [May 22, 2007, 5:26am UTC](https://www.ruby-forum.com/t/how-should-i-do-update/104755/2 "2007-05-22T05:26:52Z")

</div>

If you’re using MySQL look into ON DUPLICATE KEY UPDATE syntax which  
will allow you to do a massive insert without clobbering existing rows  
and instead update them how you see fit. To use it with ActiveRecord  
you will need to install ActiveRecord::Extensions by Zach D…

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [May 22, 2007, 5:36am UTC](https://www.ruby-forum.com/t/how-should-i-do-update/104755/3 "2007-05-22T05:36:52Z")

</div>

Thanks. I’m using postgreSQL. Your suggestion got me to searching  
though. I could push the “work” down into the database. But, then I  
thought about it longer and it would only postpone the question:  
delete or update.

PostgreSQL has Rules which one person used to implement ON DUPLICATE  
KEY UPDATE.

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [May 22, 2007, 6:08am UTC](https://www.ruby-forum.com/t/how-should-i-do-update/104755/4 "2007-05-22T06:08:54Z")

</div>

Ok.

It is probably better to do it in the DB somehow. The less data that  
passes from the DB to Rails and back, the better.

So, I’ll dig into the Rules stuff and see if I can figure it out.

Thanks again.

---

<div class="post-metadata">

**Author:** ![pedz](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/839c29/32.png) [@pedz](https://www.ruby-forum.com/u/pedz)\
**Post date:** [May 22, 2007, 5:58am UTC](https://www.ruby-forum.com/t/how-should-i-do-update/104755/5 "2007-05-22T05:58:53Z")

</div>

Well only you can answer that question. I’ve used both approaches,  
but the ON DUPLICATE KEY UPDATE advantage is an orders of magnitude  
increase in speed for the update situation. But if you can get away  
with truncating the table and just re-inserting, that is generally the  
easier thing to do. I would still look at arextensions just for the  
mass INSERT which offers a huge performance improvement when you are  
importing thousands of rows.
