From 1b35118a05e03abc4ec1cc4b0699d2438961beed Mon Sep 17 00:00:00 2001 From: Rabbit Date: Mon, 16 Dec 2024 18:19:57 +0800 Subject: [PATCH] chore: update cell_outputs query (#2349) --- app/workers/generate_udt_hourly_statistic_worker.rb | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/app/workers/generate_udt_hourly_statistic_worker.rb b/app/workers/generate_udt_hourly_statistic_worker.rb index 3f8f57bbb..74c9804a5 100644 --- a/app/workers/generate_udt_hourly_statistic_worker.rb +++ b/app/workers/generate_udt_hourly_statistic_worker.rb @@ -43,12 +43,10 @@ def generate_statistics(start_time) def calc_amount(udt) inputs_amount = 0 outputs_amount = 0 - ckb_transaction_ids = udt.ckb_transactions.map(&:id) - ckb_transaction_ids.each_slice(5000) do |ids| - batch_inputs_amount = CellOutput.select(:udt_amount).where(consumed_by_id: ids).map(&:udt_amount).compact - inputs_amount += batch_inputs_amount.sum - batch_outputs_amount = CellOutput.select(:udt_amount).where(ckb_transaction_id: ids).map(&:udt_amount).compact - outputs_amount += batch_outputs_amount.sum + udt.ckb_transactions.includes(:cell_outputs).find_in_batches(batch_size: 1000) do |transactions| + ids = transactions.map(&:id) + inputs_amount += CellOutput.select(:udt_amount).where(consumed_by_id: ids).sum(:udt_amount) + outputs_amount += CellOutput.select(:udt_amount).where(ckb_transaction_id: ids).sum(:udt_amount) end [inputs_amount, outputs_amount].max end