From 223658a2c4ce2accd315e45bb6b0a0ce2566aeb4 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Sun, 8 Dec 2019 17:16:37 +0300 Subject: [PATCH] Speedup blocks_without_reward_query --- CHANGELOG.md | 3 +-- apps/explorer/lib/explorer/chain/block.ex | 18 +++++++++++++++--- ..._block_rewards_block_hash_partial_index.exs | 9 +++++++++ 3 files changed, 25 insertions(+), 5 deletions(-) create mode 100644 apps/explorer/priv/repo/migrations/20191208135613_block_rewards_block_hash_partial_index.exs diff --git a/CHANGELOG.md b/CHANGELOG.md index 92c76dff99..370ef7cef7 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,11 @@ ### Features ### Fixes +- [#2915](https://github.com/poanetwork/blockscout/pull/2915) - Speedup of blocks_without_reward_query - [#2906](https://github.com/poanetwork/blockscout/pull/2906) - fix address sum cache - - [#2902](https://github.com/poanetwork/blockscout/pull/2902) - Offset in blocks retrieval for average block time ### Chore - - [#2896](https://github.com/poanetwork/blockscout/pull/2896) - Disable Parity websockets tests diff --git a/apps/explorer/lib/explorer/chain/block.ex b/apps/explorer/lib/explorer/chain/block.ex index 32905cf076..83aecafa9f 100644 --- a/apps/explorer/lib/explorer/chain/block.ex +++ b/apps/explorer/lib/explorer/chain/block.ex @@ -116,11 +116,23 @@ defmodule Explorer.Chain.Block do end def blocks_without_reward_query do + consensus_blocks_query = + from( + b in __MODULE__, + where: b.consensus == true + ) + + validator_rewards = + from( + r in Reward, + where: r.address_type == ^"validator" + ) + from( - b in __MODULE__, - left_join: r in Reward, + b in subquery(consensus_blocks_query), + left_join: r in subquery(validator_rewards), on: [block_hash: b.hash], - where: is_nil(r.block_hash) and b.consensus == true + where: is_nil(r.block_hash) ) end diff --git a/apps/explorer/priv/repo/migrations/20191208135613_block_rewards_block_hash_partial_index.exs b/apps/explorer/priv/repo/migrations/20191208135613_block_rewards_block_hash_partial_index.exs new file mode 100644 index 0000000000..0b0fa86a4a --- /dev/null +++ b/apps/explorer/priv/repo/migrations/20191208135613_block_rewards_block_hash_partial_index.exs @@ -0,0 +1,9 @@ +defmodule Explorer.Repo.Migrations.BlockRewardsBlockHashPartialIndex do + use Ecto.Migration + + def change do + execute( + "CREATE INDEX IF NOT EXISTS block_rewards_block_hash_partial_index on block_rewards(block_hash) WHERE address_type='validator';" + ) + end +end