ruby on rails - Delete 1000k records from table asynchronously using sidekiq -
i want delete more 1000k records (some times millions of records) table. feel safe asynchronously using sidekiq , redis, using ruby , sinatra.
when should call sidekiq's "perform" method? need run loop of records need delete? can explain how records deleted put in queue using sidekiq?
here code, please check , let me know whether following right code.
in user controller:
def delete_user deleteuserworker.perform_async(@client_info, @input_params) end
in lib/workers/deleteuser_worker.rb
class deleteuserworker include sidekiq::worker def perform(client_info, input_params) # executes query , fetches non-active records users table. @delete_users = user.where("status != ? , enddate < ?", "active", 3.months.ago ) @delete_users.each |user| user.destroy! end end end
if you're going iterate on millions of records should use find_each or find_in_batches instead of each (since .each try instantiate objects @ once):
@delete_users.find_each |user| user.destroy! end
more info: http://api.rubyonrails.org/classes/activerecord/batches.html
Comments
Post a Comment