From 9cb05b7ff064ec998634deb9f2387e723556d1bd Mon Sep 17 00:00:00 2001 From: Etienne Donneger Date: Thu, 30 May 2024 17:09:40 -0400 Subject: [PATCH] Always use `transfers_block_num` for `transfers` by default (#36) 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. --- src/usage.ts | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/usage.ts b/src/usage.ts index 1ed66ea..19bc475 100644 --- a/src/usage.ts +++ b/src/usage.ts @@ -34,16 +34,14 @@ export async function makeUsageQuery(ctx: Context, endpoint: UsageEndpoints, use query += `SELECT * FROM `; const q = query_params as ValidUserParams; - 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 @@ -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}";