Merge pull request #1124 from poanetwork/frg-fix-token-transfers-performance-issue
Fix performance issue at token transfers tab on Token's pagepull/1148/head
commit
6783fe572f
@ -0,0 +1,18 @@ |
||||
defmodule Explorer.Repo.Migrations.AddBlockNumberToTokenTransfers do |
||||
@moduledoc """ |
||||
Use `priv/repo/migrations/scripts/20181121170616_token_transfers_update_block_number_in_batches.sql` to migrate data. |
||||
|
||||
```sh |
||||
mix ecto.migrate |
||||
psql -d $DATABASE -a -f priv/repo/migrations/scripts/20181121170616_token_transfers_update_block_number_in_batches.sql |
||||
``` |
||||
""" |
||||
|
||||
use Ecto.Migration |
||||
|
||||
def change do |
||||
alter table(:token_transfers) do |
||||
add(:block_number, :integer) |
||||
end |
||||
end |
||||
end |
@ -0,0 +1,39 @@ |
||||
DO $$ |
||||
DECLARE |
||||
row_count integer; |
||||
batch_size integer := 100000; -- HOW MANY ITEMS WILL BE UPDATED AT TIME |
||||
affected integer; |
||||
BEGIN |
||||
RAISE NOTICE 'Counting items to be updated'; |
||||
|
||||
row_count := (SELECT COUNT(*) FROM token_transfers WHERE block_number IS NULL); |
||||
|
||||
RAISE NOTICE '% items', row_count; |
||||
|
||||
WHILE row_count > 0 LOOP |
||||
WITH cte AS ( |
||||
SELECT |
||||
t.hash, |
||||
t.block_number |
||||
FROM token_transfers AS tt |
||||
INNER JOIN transactions AS t ON t.hash = tt.transaction_hash |
||||
WHERE tt.block_number IS NULL |
||||
LIMIT batch_size |
||||
) |
||||
UPDATE token_transfers |
||||
SET |
||||
block_number = cte.block_number |
||||
FROM cte |
||||
WHERE token_transfers.transaction_hash = cte.hash; |
||||
|
||||
GET DIAGNOSTICS affected = ROW_COUNT; |
||||
RAISE NOTICE '-> % token transfers updated!', affected; |
||||
|
||||
-- UPDATES THE COUNTER SO IT DOESN'T TURN INTO AN INFINITE LOOP |
||||
row_count := row_count - batch_size; |
||||
|
||||
RAISE NOTICE '-> % items missing to update', row_count; |
||||
|
||||
CHECKPOINT; -- COMMITS THE BATCH UPDATES |
||||
END LOOP; |
||||
END $$; |
Loading…
Reference in new issue