我有以下关系数据库:低内存破坏操作
class Search
has_many :items , dependent: :destroy
end
class Item
has_many :sub_items , dependent: :destroy
end
class SubItem
end
项目的范围可以从100到10000。 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
以减少加载的内存,因为我销毁了它所以我所关心的是我需要从数据库中删除的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)
还有什么我可以做的,以消除内存使用? (除了减少批量大小的明显解决方案)
谢谢,会做。如我在这里建议的,我将实现自己的依赖于销毁回调。 –