sql - Updating many rows from a hash without just looping in Rails -


I have a hash that is more than thousand key / value pairs.

I have a database table with several thousand rows.

Update force on the table based on the hash is very easy to do this. Like:

  my_hash.each {| Key, value | Model.update_all ("column2 = # {value}", "column1 = # {key}"}}  

but it will be more than a thousand SQL update statements.

Is there a good way to do this with (or just some) update statements (in Rail)?

Fastest Way - In any language, it is not known how to do this especially in rail Is done - your in-memory hash will be dump in a temporary table in the database, in which there are columns for the keys of your hash only and with values, then issue an UPDATE command simultaneously, which will present your table with a new temporary table Link to update with

Check the query plan to ensure that the adapter works with it, before creating a temporary index update, if not on your temporary table, create the index itself.


Comments

Popular posts from this blog

MySql variables and php -

url rewriting - How to implement the returnurl like SO in PHP? -

Which Python client library should I use for CouchdB? -