Skip to content

Commit

Permalink
Always use transfers_block_num for transfers by default (#36)
Browse files Browse the repository at this point in the history
When querying with no parameters, the default is to query `transfer_events`.
However since the `ORDER BY` clause specify the `block_number` it will slow
down the query significantly.

Changing the default to always use the `transfers_block_num` MV table helps
solve the issue.
  • Loading branch information
0237h committed May 30, 2024
1 parent 8fc6cf4 commit 9cb05b7
Showing 1 changed file with 3 additions and 5 deletions.
8 changes: 3 additions & 5 deletions src/usage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,14 @@ export async function makeUsageQuery(ctx: Context, endpoint: UsageEndpoints, use
query += `SELECT * FROM `;

const q = query_params as ValidUserParams<typeof endpoint>;
if (q.block_range) {
query += `transfers_block_num `;
} else if (q.from) {
if (q.from) {
query += `transfers_from `;
} else if (q.to) {
query += `transfers_to `;
} else if (q.contract || q.symcode) {
query += `transfers_contract `;
} else {
query += `transfer_events `;
query += `transfers_block_num `;
}

// FINAL increases ClickHouse query response time significantly when lots of data needs merging
Expand All @@ -64,7 +62,7 @@ export async function makeUsageQuery(ctx: Context, endpoint: UsageEndpoints, use
query += ` ${k} == {${k}: String} AND`;
query = query.substring(0, query.lastIndexOf(' ')); // Remove last item ` AND`

query += endpoint == "/holders" ? " ORDER BY value DESC" : " ORDER BY block_num DESC";
query += endpoint == "/holders" ? " ORDER BY value DESC" : " ORDER BY block_num";
query += " LIMIT {limit: int}";
query += " OFFSET {offset: int}";

Expand Down

0 comments on commit 9cb05b7

Please sign in to comment.