Skip to content

Instantly share code, notes, and snippets.

@pnispel
Created October 13, 2017 23:09
Show Gist options
  • Save pnispel/1a3b752f2bfb39767e243cc6c4177a17 to your computer and use it in GitHub Desktop.
Save pnispel/1a3b752f2bfb39767e243cc6c4177a17 to your computer and use it in GitHub Desktop.
def self.find_optimal_batch_size
times = []
[100, 1_000, 10_000, 100_000].each do |batch_size|
times << [timed_query(batch_size), batch_size]
end
times.sort
end
def self.timed_query(batch_size)
max_batches = (LIMIT / BATCH_SIZE.to_f).ceil
Benchmark.realtime do
batch_count = 0
govcloud_company_ids = Company.where(use_govcloud: true).select(:id)
ProstoreFile.unscoped.select(:id).where(company_id: govcloud_company_ids).
find_in_batches(batch_size: batch_size) do |prostore_files|
ProstoreFile.unscoped.where(id: prostore_files.map(&:id)).update_all(storage_profile_key: nil)
batch_count +=1
break if batch_count == max_batches
end
batch_count = 0
ProstoreFile.unscoped.select(:id).where.not(company_id: govcloud_company_ids).
find_in_batches(batch_size: batch_size) do |prostore_files|
ProstoreFile.unscoped.where(id: prostore_files.map(&:id)).update_all(storage_profile_key: nil)
batch_count +=1
break if batch_count == max_batches
end
batch_count = 0
ProstoreFile.unscoped.select(:id).where(company_id: nil).
find_in_batches(batch_size: batch_size) do |prostore_files|
ProstoreFile.unscoped.where(id: prostore_files.map(&:id)).update_all(storage_profile_key: nil)
batch_count +=1
break if batch_count == max_batches
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment