을 파괴 나는 다음과 같은 관계형 DB가 :낮은 메모리는 작업
class Search
has_many :items , dependent: :destroy
end
class Item
has_many :sub_items , dependent: :destroy
end
class SubItem
end
항목은 100에서 10,000까지 다양 할 수 있습니다. SumItems는 평균 약 100입니다. 내 기억 낮게 유지하려고
내가 사용하는 다음
클래스 검색
before_destroy :destroy_items_in_batches
def destroy_items_in_batches
self.try(:items).try(:in_batches, { of: 300 }).try(:destroy_all)
end
클래스 그것은 아직 충분하지
before_destroy :destroy_transactions_in_batches
def destroy_transactions_in_batches
self.try(:sub_items).try(:in_batches, { of: 100 }).try(:destroy_all)
end
항목, 그래서 쿼리를 select
추가하여로드 된 메모리를 줄이기 때문에 나는 그것을 파괴하고 있습니다. nyway는 데이터베이스에서 제거해야하는 ID입니다.
self.try(:items).try(:select,:id).try(:in_batches, { of: 300 }).try(:destroy_all)
self.try(:sub_items).try(:select,:id).try(:in_batches, { of: 100 }).try(:destroy_all)
다른 방법으로 메모리 사용을 제거 할 수 있습니까? (배치 크기를 줄이는 명백한 해결 방법 외에)
감사합니다. 여기에 제안 된 것처럼 파괴 콜백에 대한 의존성을 구현할 것입니다. –