From 81f7680fc898175373ef21772bfe8bbce833222c Mon Sep 17 00:00:00 2001 From: Tsvetozar Penov Date: Mon, 13 Nov 2023 15:50:53 +0100 Subject: [PATCH] Improve query speed and add to examples --- lib/sanbase/balances/balance.ex | 22 +++++++++++++--------- lib/sanbase/balances/balance_sql_query.ex | 23 +++++++++++++++-------- lib/sanbase/run_examples.ex | 15 ++++++++++++++- 3 files changed, 42 insertions(+), 18 deletions(-) diff --git a/lib/sanbase/balances/balance.ex b/lib/sanbase/balances/balance.ex index 9689882936..2586ec3611 100644 --- a/lib/sanbase/balances/balance.ex +++ b/lib/sanbase/balances/balance.ex @@ -307,18 +307,22 @@ defmodule Sanbase.Balance do def current_balance_top_addresses(slug, opts) do with {:ok, {decimals, infrastructure, blockchain}} <- info_by_slug(slug), {:ok, table} <- realtime_balances_table(slug, infrastructure) do - query_struct = top_addresses_query(slug, decimals, blockchain, table, opts) - - ClickhouseRepo.query_transform(query_struct, fn [address, balance] -> - %{ - address: address, - infrastructure: infrastructure, - balance: balance - } - end) + current_balance_top_addresses(slug, decimals, infrastructure, blockchain, table, opts) end end + def current_balance_top_addresses(slug, decimals, infrastructure, blockchain, table, opts) do + query_struct = top_addresses_query(slug, decimals, blockchain, table, opts) + + ClickhouseRepo.query_transform(query_struct, fn [address, balance] -> + %{ + address: address, + infrastructure: infrastructure, + balance: balance + } + end) + end + def realtime_balances_table_or_nil(slug, infr) do case realtime_balances_table(slug, infr) do {:ok, table} -> {:ok, table} diff --git a/lib/sanbase/balances/balance_sql_query.ex b/lib/sanbase/balances/balance_sql_query.ex index 7bdb079b1a..aa6841c803 100644 --- a/lib/sanbase/balances/balance_sql_query.ex +++ b/lib/sanbase/balances/balance_sql_query.ex @@ -359,16 +359,23 @@ defmodule Sanbase.Balance.SqlQuery do sql = """ SELECT address, balance FROM ( - SELECT address, argMax(balance, dt) / pow(10, {{decimals}}) AS balance - FROM #{table} - PREWHERE - addressType = 'normal' - GROUP BY address + SELECT + ebr.address, + argMax(ebr.balance, ebr.dt) / pow(10, {{decimals}}) AS balance + FROM #{table} AS ebr + WHERE (ebr.address GLOBAL IN ( + SELECT address + FROM eth_top_holders_daily + WHERE value > 1e10 AND(dt = toStartOfDay(today() - toIntervalDay(1))) AND (rank > 0) + ORDER BY value #{direction} + LIMIT {{limit}}*2 + )) AND (ebr.addressType = 'normal') + GROUP BY ebr.address ) #{labels_join_str} - WHERE balance > 1e-10 ORDER BY balance #{direction} - LIMIT {{limit}} OFFSET {{offset}} + LIMIT {{limit}} + OFFSET {{offset}} """ Sanbase.Clickhouse.Query.new(sql, params) @@ -397,7 +404,7 @@ defmodule Sanbase.Balance.SqlQuery do FROM #{table} PREWHERE assetRefId = (SELECT asset_ref_id FROM asset_metadata FINAL WHERE name = {{slug}} LIMIT 1) AND - addressType = 'normal' + addressType = 'normal' AND (dt > (now() - toIntervalDay(1))) GROUP BY address ) #{labels_join_str} diff --git a/lib/sanbase/run_examples.ex b/lib/sanbase/run_examples.ex index 746568db14..4df9af98a8 100644 --- a/lib/sanbase/run_examples.ex +++ b/lib/sanbase/run_examples.ex @@ -24,7 +24,8 @@ defmodule Sanbase.RunExamples do :transfers, :san_burn_credit_transactions, :signals, - :additional_filters + :additional_filters, + :top_addresses ] @from ~U[2023-01-01 00:00:00Z] @@ -523,6 +524,18 @@ defmodule Sanbase.RunExamples do infrastructure: "ETH", address: @null_address }) + end + + defp do_run(:top_addresses) do + {:ok, _} = + Sanbase.Balance.current_balance_top_addresses( + "ethereum", + 18, + "ETH", + "ethereum", + "eth_balances_realtime", + labels: ["whale_usd_balance"] + ) {:ok, :success} end