Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

export transactions csv export slow #336

Closed
rabbitz opened this issue Jun 2, 2023 · 2 comments
Closed

export transactions csv export slow #336

rabbitz opened this issue Jun 2, 2023 · 2 comments
Assignees

Comments

@rabbitz
Copy link

rabbitz commented Jun 2, 2023

Exporting transactions to csv is too slow, queries take too long, and the web interface doesn't give any hints.

Related issues:

#41

Related pull requests:

Magickbase/ckb-explorer-frontend#9
nervosnetwork/ckb-explorer#1241

@rabbitz
Copy link
Author

rabbitz commented Jun 2, 2023

Reason

The table only needs to export the capacity corresponding to cell_inputs and cell_outputs corresponding to ckb_transaction, but the display_inputs and display_outputs that are called now will cause additional query of table blocks and udts.

Solution

  1. Use limit(5000).find_in_batches(batch_size: 1000) query according to AccountBook according to ckb address
  2. Avoid N+1 queries, use ckb_transactions.includes(:inputs, :outputs)
  3. The inputs = ckb_transaction.display_inputs method of the ckb_transaction object is rewritten as

https://github.com/nervosnetwork/ckb-explorer/blob/develop/app/controllers/api/v1/address_transactions_controller.rb#L62

inputs =
   if ckb_transaction.is_cellbase
     return [""]
   else
     cell_inputs_for_display = ckb_transaction.inputs.to_a.sort_by(&:id)
     cell_inputs_for_display.map(&:capacity)
   end
  1. The outputs = ckb_transaction.display_outputs method of the ckb_transaction object is rewritten as

https://github.com/nervosnetwork/ckb-explorer/blob/develop/app/controllers/api/v1/address_transactions_controller.rb#L63

cell_outputs_for_display = ckb_transaction.outputs.to_a.sort_by(&:id)
outputs = cell_outputs_for_display.map(&:capacity)

Effect

Locally query the staging environment address ckt1qpuljza4azfdsrwjzdpea6442yfqadqhv7yzfu5zknlmtusm45hpuqgp07xsfzppphcyt4n5plw47tgzqv76gvgpqqyu9k6c Export date 2023-05-05 to 2023-06-01 The number of ckb_transactions records is 2173

--> Api::V1::AddressTransactionsController download_csv
       user system total real
   1.151034 0.345807 1.496841 ( 12.128878)

@ShiningRay @Keith-CY @zmcNotafraid

@rabbitz rabbitz moved this from 🆕 New to 🏗 In Progress in CKB Explorer Jun 5, 2023
@rabbitz
Copy link
Author

rabbitz commented Jun 5, 2023

Related PR: nervosnetwork/ckb-explorer#1301

@rabbitz rabbitz moved this from 🏗 In Progress to 👀 Testing in CKB Explorer Jun 7, 2023
@Danie0918 Danie0918 moved this from 👀 Testing to ✅ Done in CKB Explorer Jun 12, 2023
# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
Archived in project
Development

No branches or pull requests

2 participants