Speedup address_to_logs query

pull/2924/head
Victor Baranov 5 years ago
parent 764a95c7e5
commit 12905cf9a5
  1. 3
      CHANGELOG.md
  2. 23
      apps/explorer/lib/explorer/chain.ex
  3. 9
      apps/explorer/priv/repo/migrations/20191218120138_logs_block_number_index_index.exs

@ -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

@ -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)

@ -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
Loading…
Cancel
Save