From 88439ae38c50c72c5a026431b40b533961e86d73 Mon Sep 17 00:00:00 2001 From: Ayrat Badykov Date: Thu, 14 Mar 2019 15:13:33 +0300 Subject: [PATCH] increase query speed --- .../templates/address/overview.html.eex | 3 ++- .../lib/block_scout_web/views/address_view.ex | 4 +++- apps/explorer/lib/explorer/chain.ex | 21 +++++++++++++------ 3 files changed, 20 insertions(+), 8 deletions(-) diff --git a/apps/block_scout_web/lib/block_scout_web/templates/address/overview.html.eex b/apps/block_scout_web/lib/block_scout_web/templates/address/overview.html.eex index 42906111ef..01b8929d58 100644 --- a/apps/block_scout_web/lib/block_scout_web/templates/address/overview.html.eex +++ b/apps/block_scout_web/lib/block_scout_web/templates/address/overview.html.eex @@ -36,8 +36,9 @@ <%= if contract?(@address) do %> + <%= gettext(">=") %> - <%= Cldr.Number.to_string!(incoming_transaction_count(@address), format: "#,###") %> + <%= incoming_transaction_count(@address) %> <%= gettext("Incoming Transactions") %> <% else %> diff --git a/apps/block_scout_web/lib/block_scout_web/views/address_view.ex b/apps/block_scout_web/lib/block_scout_web/views/address_view.ex index e527757b23..fe6245ec54 100644 --- a/apps/block_scout_web/lib/block_scout_web/views/address_view.ex +++ b/apps/block_scout_web/lib/block_scout_web/views/address_view.ex @@ -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 diff --git a/apps/explorer/lib/explorer/chain.ex b/apps/explorer/lib/explorer/chain.ex index 6d8a532b58..33a84bc0c9 100644 --- a/apps/explorer/lib/explorer/chain.ex +++ b/apps/explorer/lib/explorer/chain.ex @@ -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`