0
表中有4萬條記錄,我想分到4組。 第一種方式:使用mysql的兩個分組選擇方法,哪個更快,哪個更好?
def prepare_params(div, mod)
results = []
Account.find_each do |account|
next unless account.id % div == mod
results << [account.id, account.name]
end
results
end
secord方式:
def prepare_params(block, block_size)
results = Account.limit(block_size).offset(block_size * (block - 1)).pluck(:id, :name)
end
這兩使用MySQL的分組選擇的方法,這是更快,哪個更好?
['require'benchmark''](http://ruby-doc.org/stdlib-2.2.0/libdoc/benchmark/rdoc/Benchmark.html)。順便說一句,第二個肯定更好,因爲它不會在紅寶石內部迭代40K記錄。 – mudasobwa