Skip to content

Instantly share code, notes, and snippets.

@KingofHamyang
Last active November 27, 2019 07:21
Show Gist options
  • Save KingofHamyang/2561b131302a8c25d8c8847d091dda89 to your computer and use it in GitHub Desktop.
Save KingofHamyang/2561b131302a8c25d8c8847d091dda89 to your computer and use it in GitHub Desktop.
Shop.find_each do |shop|
botem_weight = shop.botem_weight;
actual_weight = shop.wifi_actual_weight;
Shop::WeightHistory.find_or_create_by(shop_id: shop.id, weight: botem_weight, weight_type: "botem", date: shop.created_at.strftime("%Y-%m-%d"));
Shop::WeightHistory.find_or_create_by(shop_id: shop.id, weight: actual_weight, weight_type: "actual", date: shop.created_at.strftime("%Y-%m-%d"));
end
Shop.find_each do |shop|
shop.measures.where("date >= ?", shop.created_at.strftime("%Y-%m-%d").to_date).each do |measure|
botem_weight = measure.wifi_botem_ratio <= 1E-5 ? 0 : 1/measure.wifi_botem_ratio
actual_weight = shop.wifi_actual_weight
Shop::Weight.find_or_create_by!(shop_id: shop.id, date: measure.date, actual: actual_weight, botem: botem_weight)
end
end
@KingofHamyang
Copy link
Author

KingofHamyang commented Nov 8, 2019

rails 4.x 에서 find_each를 쓸 때 해당 모델의 primary key가 Numerical 이 아니면 돌아가지 않는 버그가 있습니다. Measure의 경우 primary key가 date,shop_id이다 보니 작동하지 않습니다. 따라서 where과 each로 했습니다. 이럴경우 메모리에 저 튜플들을 다 올리게 됩니다
1년 기준 -> 365 * shop갯수(약 3000개) -> 약 1,095,000개 -> 튜플하나 1kb 가정 -> 약 1.04GB

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment