Skip to content

Commit

Permalink
Use UNION on from/to for account transfers (#80)
Browse files Browse the repository at this point in the history
Optimize usage of Clickhouse table indexes
  • Loading branch information
0237h authored Jan 20, 2025
1 parent eddea58 commit cb436aa
Showing 1 changed file with 2 additions and 5 deletions.
7 changes: 2 additions & 5 deletions src/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,10 @@ export async function makeUsageQuery(ctx: Context, endpoint: UsageEndpoints, use

query += ` ${filters} ORDER BY block_num DESC`;
} else if (endpoint == "/account/transfers") {
let table = "transfers_from";
if (filters.includes("to") && !filters.includes("from"))
table = "transfers_to"

query +=
`SELECT * FROM`
+ ` (SELECT DISTINCT * FROM ${table} ${/from|to/.test(filters) ? "" : "WHERE ((from = {account: String}) OR (to = {account: String}))"})`
+ ` ((SELECT DISTINCT * FROM transfers_from WHERE (from = {account: String}))`
+ ` UNION ALL (SELECT DISTINCT * FROM transfers_to WHERE (to = {account: String})))`
+ ` ${filters} ORDER BY block_num DESC`;
} else if (endpoint == "/transfers/id") {
query += `SELECT * FROM transfer_events ${filters} ORDER BY action_index`;
Expand Down

0 comments on commit cb436aa

Please sign in to comment.