Merge pull request #2924 from poanetwork/vb-speedup-address-to-logs-query

Speedup address to logs query
pp-pending-blocks-ops
Victor Baranov 5 years ago committed by GitHub
commit 2a807771ba
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 1
      CHANGELOG.md
  2. 23
      apps/explorer/lib/explorer/chain.ex
  3. 7
      apps/explorer/priv/repo/migrations/20191218120138_logs_block_number_index_index.exs

@ -5,6 +5,7 @@
### Fixes
- [#2928](https://github.com/poanetwork/blockscout/pull/2928) - Speedup pending block ops int txs to fetch query
- [#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

@ -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,7 @@
defmodule Explorer.Repo.Migrations.LogsBlockNumberIndexIndex do
use Ecto.Migration
def change do
create_if_not_exists(index(:logs, ["block_number DESC, index DESC"]))
end
end
Loading…
Cancel
Save