ruby on rails - Update huge data in rake task -
i have model named pagedensity
has 5m rows.
when created project pagedensity
table stored float of 5 decimal precision in density
coulmn.
now requirement changed round 2 decimal places.
i wrote task round densities
makes system heavy stucks. can't use query
rounding bit change 0.57500
rounded 0.57
, 0.57600
rounded 0.58
.
what have tried far simply:
task round_densities: :environment application_object = applicationcontroller.new time = benchmark.realtime activerecord::base.transaction pagedensity.all.each {|p| p.update_attributes(density: application_object.round_number(p.density))} end end puts '***************************************' puts "total time consumed #{time} seconds" puts '***************************************' end
and tried make query rounding failed:
select round(0.00500, 2) #this returns 0.01 should return 0.00
i using postgres
idea make psql query
or using rails
?
it sounds rounding requirement 0.001 off normal rounding be.
in case think can run sql update:
update page_densities set density = round(density - 0.001, 2)
this round this:
0.011 => round(0.010, 2) => 0.01 0.015 => round(0.014, 2) => 0.01 0.016 => round(0.015, 2) => 0.02 0.02 => round(0.019, 2) => 0.02
Comments
Post a Comment