Skip to content
This repository was archived by the owner on Nov 25, 2020. It is now read-only.

Commit 1abd9f7

Browse files
committed
tune queries for performance
1 parent d8f77db commit 1abd9f7

File tree

2 files changed

+28
-18
lines changed

2 files changed

+28
-18
lines changed

lib/blockchain_api/query/stats.ex

Lines changed: 20 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ defmodule BlockchainAPI.Query.Stats do
2525

2626
def list() do
2727
Cache.Util.get(:stats_cache, :stats, &set_list/0, @cache_timeout)
28+
# {:commit, data} = set_list()
29+
# data
2830
end
2931

3032
defp set_list() do
@@ -184,15 +186,16 @@ defmodule BlockchainAPI.Query.Stats do
184186
defp query_frequent_concensus_members(start, finish) do
185187
count_query =
186188
from(
187-
cm in ConsensusMember,
188-
inner_join: et in ElectionTransaction,
189-
on: et.id == cm.election_transactions_id,
189+
b in Block,
190190
inner_join: tx in Transaction,
191-
on: tx.hash == et.hash,
192-
inner_join: b in Block,
193191
on: b.height == tx.block_height,
192+
inner_join: et in ElectionTransaction,
193+
on: tx.hash == et.hash,
194+
inner_join: cm in ConsensusMember,
195+
on: et.id == cm.election_transactions_id,
194196
where: b.time >= ^start,
195197
where: b.time <= ^finish,
198+
where: tx.type == "election",
196199
group_by: cm.address,
197200
select: %{
198201
count: fragment("count(*)"),
@@ -230,11 +233,12 @@ defmodule BlockchainAPI.Query.Stats do
230233
defp query_top_grossing_hotspots(start, finish) do
231234
sum_query =
232235
from(
233-
rt in RewardTxn,
236+
b in Block,
234237
inner_join: tx in Transaction,
235-
on: tx.hash == rt.rewards_hash,
236-
inner_join: b in Block,
237238
on: b.height == tx.block_height,
239+
inner_join: rt in RewardTxn,
240+
on: tx.hash == rt.rewards_hash,
241+
where: tx.type == "rewards",
238242
where: b.time >= ^start,
239243
where: b.time <= ^finish,
240244
where: not is_nil(rt.gateway),
@@ -276,16 +280,8 @@ defmodule BlockchainAPI.Query.Stats do
276280
distance_query =
277281
from(
278282
pw in POCWitness,
279-
inner_join: pe in POCPathElement,
280-
on: pe.id == pw.poc_path_elements_id,
281-
inner_join: rt in POCReceiptsTransaction,
282-
on: rt.hash == pe.poc_receipts_transactions_hash,
283-
inner_join: tx in Transaction,
284-
on: tx.hash == rt.hash,
285-
inner_join: b in Block,
286-
on: b.height == tx.block_height,
287-
where: b.time >= ^start,
288-
where: b.time <= ^finish,
283+
where: pw.timestamp / 1000000000 >= ^start,
284+
where: pw.timestamp / 1000000000 <= ^finish,
289285
select: %{
290286
gateway: pw.gateway,
291287
distance: pw.distance,
@@ -327,4 +323,10 @@ defmodule BlockchainAPI.Query.Stats do
327323
interval -> interval |> Decimal.to_float()
328324
end
329325
end
326+
327+
defp print_sql(queryable) do
328+
{query, params} = Ecto.Adapters.SQL.to_sql(:all, Repo, queryable)
329+
IO.puts("#{query}, #{inspect(params)}")
330+
queryable
331+
end
330332
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
defmodule BlockchainAPI.Repo.Migrations.AddStatsIndexes do
2+
use Ecto.Migration
3+
4+
def change do
5+
create index(:transactions, [:type])
6+
create index(:consensus_members, [:election_transactions_id])
7+
end
8+
end

0 commit comments

Comments
 (0)