increase query speed

pull/1562/head
Ayrat Badykov 6 years ago
parent 345ce79e47
commit 88439ae38c
No known key found for this signature in database
GPG Key ID: B44668E265E9396F
  1. 3
      apps/block_scout_web/lib/block_scout_web/templates/address/overview.html.eex
  2. 4
      apps/block_scout_web/lib/block_scout_web/views/address_view.ex
  3. 21
      apps/explorer/lib/explorer/chain.ex

@ -36,8 +36,9 @@
<span>
<span class="address-detail-item">
<%= if contract?(@address) do %>
<%= gettext(">=") %>
<span data-selector="transaction-count">
<%= Cldr.Number.to_string!(incoming_transaction_count(@address), format: "#,###") %>
<%= incoming_transaction_count(@address) %>
</span>
<%= gettext("Incoming Transactions") %>
<% else %>

@ -214,7 +214,9 @@ defmodule BlockScoutWeb.AddressView do
def token_title(%Token{name: name, symbol: symbol}), do: "#{name} (#{symbol})"
def incoming_transaction_count(%Address{} = address) do
Chain.address_to_incoming_transaction_count(address)
count = Chain.address_to_incoming_transaction_count(address)
Cldr.Number.to_string!(count, format: "#,###")
end
def trimmed_hash(%Hash{} = hash) do

@ -51,6 +51,8 @@ defmodule Explorer.Chain do
@default_paging_options %PagingOptions{page_size: 50}
@max_incoming_transactions_count 10_000
@typedoc """
The name of an association on the `t:Ecto.Schema.t/0`
"""
@ -419,15 +421,22 @@ defmodule Explorer.Chain do
@spec address_to_incoming_transaction_count(Address.t()) :: non_neg_integer()
def address_to_incoming_transaction_count(%Address{hash: address_hash}) do
query =
from(
transaction in Transaction,
where: transaction.to_address_hash == ^address_hash
)
paging_options = %PagingOptions{page_size: @max_incoming_transactions_count}
Repo.aggregate(query, :count, :hash)
base_query =
paging_options
|> fetch_transactions()
to_address_query =
base_query
|> where([t], t.to_address_hash == ^address_hash)
Repo.aggregate(to_address_query, :count, :hash, timeout: :infinity)
end
@spec max_incoming_transactions_count() :: non_neg_integer()
def max_incoming_transactions_count, do: @max_incoming_transactions_count
@doc """
How many blocks have confirmed `block` based on the current `max_block_number`

Loading…
Cancel
Save