ruby-on-rails - Rails 使用进程移除器的代码重构来加速数据库
问题描述
我的工人阶级移除了InquiryProcess
早于x time
(默认应设置为 6 个月)。可能这将是一个大数据规模,所以有没有机会用下面的代码加速删除?
class OldProcessRemover
def initialize(date: 6.months.ago)
@date = date
end
attr_reader :date
def call
remove_loan
remove_checking_account
end
private
def remove_loan
loan_template = InquiryTemplate.find_by(inquiry_process_name: InquiryTemplate::LOAN_APPLICATION_PROCESS_NAME)
loan_template.inquiry_processes.where('created_at <= ?', date).each(&:destroy)
end
def remove_checking_account
checking_account_template = InquiryTemplate.find_by(
inquiry_process_name: InquiryTemplate::CHECKING_ACCOUNT_OPENING_PROCESS_NAME,
)
checking_account_template.inquiry_processes.where('created_at <= ?', date).each(&:destroy)
end
end
也许我可以使用的地方find_in_batches
?我不认为这些方法是单一的责任,所以重构也会有所帮助。
解决方案
class OldProcessRemover
def initialize(date: 6.months.ago)
@date = date
end
attr_reader :date
def call
remove_loan
remove_checking_account
end
private
def remove_loan
remove_processes!(InquiryTemplate::LOAN_APPLICATION_PROCESS_NAME)
end
def remove_checking_account
remove_processes!(InquiryTemplate::CHECKING_ACCOUNT_OPENING_PROCESS_NAME)
end
def remove_processes!(process_name)
account_template = InquiryTemplate.find_by(
inquiry_process_name: process_name
)
account_template.inquiry_processes
.where('created_at <= ?', date)
.find_in_batches { |group| group.destroy_all }
end
end
我认为 using.find_in_batches { |group| group.destroy_all }
或.find_each {|record| record.destroy }
here 之间没有任何重大区别。
推荐阅读
- nao-robot - 使用 nao robot / Choregraphe 从另一个项目运行项目/脚本
- apache - 如何在 Apache mod_rewrite 中设置条件块
- typo3 - 如何仅使用斜线配置 TYPO3 9.5.5 Rootpage(修复 /1 错误)?
- php - 如何执行与变量同名的函数在laravel中求值
- java - Spring:在不打开浏览器的情况下填充 OAuth2 表单
- python - Pandas 从 Excel 读取时会更改数字
- java - 由于基础异常而无法加载连接类:com.mysql.cj.exceptions.WrongArgumentException
- reactjs - 福米克
如果使用 componentDidMount/setState,initialValue 未定义 - taskscheduler - 在管理员帐户下创建时间触发器 Windows 任务时访问被拒绝
- python - Ruby 等价于 Python chain()