From 12905cf9a521bf00992399fde585a21afaa52bca Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Wed, 18 Dec 2019 18:05:44 +0300 Subject: [PATCH 1/2] Speedup address_to_logs query --- CHANGELOG.md | 3 +-- apps/explorer/lib/explorer/chain.ex | 23 +++++++++++++------ ...18120138_logs_block_number_index_index.exs | 9 ++++++++ 3 files changed, 26 insertions(+), 9 deletions(-) create mode 100644 apps/explorer/priv/repo/migrations/20191218120138_logs_block_number_index_index.exs diff --git a/CHANGELOG.md b/CHANGELOG.md index ec51506d9c..8675c5c51e 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,12 +3,11 @@ ### Features ### Fixes +- [#2924](https://github.com/poanetwork/blockscout/pull/2924) - Speedup address to logs 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.ex b/apps/explorer/lib/explorer/chain.ex index ca4bc2a1f5..2ca7d17925 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -358,22 +358,31 @@ defmodule Explorer.Chain do base_query = from(log in Log, inner_join: transaction in Transaction, - on: - transaction.block_hash == log.block_hash and transaction.block_number == log.block_number and - transaction.hash == log.transaction_hash, - order_by: [desc: transaction.block_number, desc: transaction.index], - preload: [:transaction, transaction: [to_address: :smart_contract]], + on: transaction.hash == log.transaction_hash, + order_by: [desc: log.block_number, desc: log.index], where: transaction.block_number < ^block_number, or_where: transaction.block_number == ^block_number and transaction.index > ^transaction_index, or_where: transaction.block_number == ^block_number and transaction.index == ^transaction_index and log.index > ^log_index, - where: log.address_hash == ^address_hash and log.block_hash == transaction.block_hash, + where: log.address_hash == ^address_hash, limit: ^paging_options.page_size, select: log ) - base_query + wrapped_query = + from( + log in subquery(base_query), + inner_join: transaction in Transaction, + preload: [:transaction, transaction: [to_address: :smart_contract]], + where: + log.block_hash == transaction.block_hash and + log.block_number == transaction.block_number and + log.transaction_hash == transaction.hash, + select: log + ) + + wrapped_query |> filter_topic(options) |> Repo.all() |> Enum.take(paging_options.page_size) diff --git a/apps/explorer/priv/repo/migrations/20191218120138_logs_block_number_index_index.exs b/apps/explorer/priv/repo/migrations/20191218120138_logs_block_number_index_index.exs new file mode 100644 index 0000000000..14d0b647a0 --- /dev/null +++ b/apps/explorer/priv/repo/migrations/20191218120138_logs_block_number_index_index.exs @@ -0,0 +1,9 @@ +defmodule Explorer.Repo.Migrations.LogsBlockNumberIndexIndex do + use Ecto.Migration + + def change do + create_if_not_exists(index(:logs, ["block_number DESC, index DESC"])) + + drop_if_exists(index(:logs, [:block_number], name: "logs_block_number_index")) + end +end From 06049209f111c2fc1395e1e5c805ea84ca385a99 Mon Sep 17 00:00:00 2001 From: Victor Baranov Date: Fri, 20 Dec 2019 16:09:27 +0300 Subject: [PATCH 2/2] Remove dropping of index logs_block_number_index because it never exists in migrations now --- .../migrations/20191218120138_logs_block_number_index_index.exs | 2 -- 1 file changed, 2 deletions(-) diff --git a/apps/explorer/priv/repo/migrations/20191218120138_logs_block_number_index_index.exs b/apps/explorer/priv/repo/migrations/20191218120138_logs_block_number_index_index.exs index 14d0b647a0..735a835db9 100644 --- a/apps/explorer/priv/repo/migrations/20191218120138_logs_block_number_index_index.exs +++ b/apps/explorer/priv/repo/migrations/20191218120138_logs_block_number_index_index.exs @@ -3,7 +3,5 @@ defmodule Explorer.Repo.Migrations.LogsBlockNumberIndexIndex do def change do create_if_not_exists(index(:logs, ["block_number DESC, index DESC"])) - - drop_if_exists(index(:logs, [:block_number], name: "logs_block_number_index")) end end