Skip to content

Commit b724ae1

Browse files
committed
Optimise the ranking queries by using distinct instead of join and index on blocknum
1 parent 6962cf9 commit b724ae1

File tree

3 files changed

+13
-17
lines changed

3 files changed

+13
-17
lines changed

migrations/000002_pool.up.sql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ CREATE TABLE pools (
88
);
99

1010
CREATE TABLE pool_stats (
11-
id SERIAL PRIMARY KEY,
11+
id SERIAL NOT NULL,
1212
pool varchar(42),
1313
block_num integer,
1414
unique_users integer,
@@ -40,7 +40,7 @@ ALTER TABLE ONLY pool_stats
4040

4141

4242
CREATE TABLE pool_ledger (
43-
id SERIAL PRIMARY KEY,
43+
id SERIAL NOT NULL,
4444
block_num integer,
4545
log_id integer,
4646
pool character varying(42),

migrations/000008_debts.up.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ CREATE TABLE debt_sync (
33
);
44

55
CREATE TABLE debts (
6-
id SERIAL PRIMARY KEY,
6+
id SERIAL NOT NULL,
77
block_num integer,
88
session_id character varying(100),
99
cal_health_factor varchar(80),

migrations/000016_rankings.up.sql

Lines changed: 10 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -27,24 +27,20 @@ language plpgsql
2727
as $$
2828
DECLARE
2929
BEGIN
30-
RETURN QUERY WITH common AS
31-
(SELECT min(d1.block_num) , max(d1.block_num) , d1.session_id from debts d1
32-
JOIN (SELECT * FROM blocks WHERE timestamp > (extract(epoch from now())::bigint - $1)) b
33-
ON b.id = d1.block_num group by d1.session_id)
34-
SELECT *, (t2.new_profit-t1.old_profit) profit_usd, t1.old_collateral collateral_usd,
30+
RETURN QUERY SELECT *, (t2.new_profit-t1.old_profit) profit_usd, t1.old_collateral collateral_usd,
3531
(t2.new_profit_underlying-t1.old_profit_underlying) profit_underlying, t1.old_collateral_underlying collateral_underlying,
3632
(t2.new_profit-t1.old_profit)/(t1.old_collateral) roi FROM
37-
(SELECT
38-
d.collateral_usd old_collateral, d.profit_usd as old_profit, d.total_value_usd old_total,
33+
(SELECT distinct on (d.session_id) d.collateral_usd old_collateral, d.profit_usd as old_profit, d.total_value_usd old_total,
3934
d.collateral_underlying old_collateral_underlying, d.profit_underlying as old_profit_underlying,
40-
d.session_id sid
41-
FROM debts d JOIN common ON common.min = d.block_num AND d.session_id=common.session_id) t1
42-
JOIN (SELECT
43-
d.collateral_usd new_collateral, d.profit_usd as new_profit, d.total_value_usd new_total,
35+
d.session_id sid
36+
FROM debts d WHERE block_num >= (SELECT min(id) FROM blocks WHERE timestamp > (extract(epoch from now())::bigint - $1))
37+
order by d.session_id, block_num) t1
38+
JOIN (SELECT distinct on (d.session_id) d.collateral_usd new_collateral, d.profit_usd as new_profit, d.total_value_usd new_total,
4439
d.collateral_underlying new_collateral_underlying, d.profit_underlying as new_profit_underlying,
45-
d.session_id, common.max current_block
46-
FROM debts d JOIN common ON common.max = d.block_num AND d.session_id=common.session_id) t2
47-
ON t1.sid = t2.session_id;
40+
d.session_id, block_num current_block
41+
FROM debts d WHERE block_num >= (SELECT min(id) FROM blocks WHERE timestamp > (extract(epoch from now())::bigint - $1))
42+
order by d.session_id, block_num DESC) t2
43+
ON t1.sid = t2.session_id;
4844
END $$;
4945

5046
CREATE MATERIALIZED VIEW ranking_7d AS

0 commit comments

Comments
 (0)