On Saturday, December 5, 2015 at 9:10:43 AM UTC-8, Mike Pastore wrote:
>
> Hi all,
>
> I'm performing batch updates like so:
>
> World.db.transaction do
>   worlds = World
>     .where(:id => updates.keys.sort!)
>     .for_update
>     .all
>
>   worlds
>     .each { |w| w.randomNumber = updates[w.id] }
>
>   World.dataset
>     .on_duplicate_key_update(:randomNumber)
>     .multi_insert(worlds)
> end
>
> `updates` is a hash where the key is the PK of the record to be updated 
> and the value is hopefully self-explanatory. I'm sorting the keys to 
> prevent deadlock on the MySQL side and loading the models into memory with 
> `#all` because the resultset is quite small and I'd like this to be as 
> performant as possible.
>
> My questions are as follows:
>
>    - If I submit a PR to expose the `Dataset#on_duplicate_key_update` 
>    method to Models, would it be accepted? I hate having to write 
>    `Model.dataset`!
>    
> No.  You can add the dataset method to all models via:

  Sequel::Plugins.def_dataset_methods(Sequel::Model, 
[:on_duplicate_key_update])
 

>
>    - Is there a quick and easy way to mark my models as "clean" after the 
>    batch update is successfully applied?
>    
> I'm not sure  what you mean.  If you mean individual records, adding a 
column that stores whether the record is clean should work.  If you mean 
for the whole table, maybe another table with a single column that stores 
the tables that have already been cleaned.
 

>
>    - Are there any other style or performance suggestions you can make? 
>    This is for some Sinatra/Sequel tests I'm working on for 
>    FrameworkBenchmarks[1].
>    
> Well, for significantly better performance, you'd probably have to drop to 
lower levels.  As a minor speedup, you could pass the block you are passing 
to worlds.each to all, that would eliminate the method call to worlds.each. 
 I'm not sure the sort! is necessary.  I doubt it would make a difference 
on PostgreSQL, but maybe on MySQL it does.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/d/optout.

Reply via email to