-
Notifications
You must be signed in to change notification settings - Fork 0
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Station Public Dashboard - optimise "Leaderboard - FIL Earned" #178
Comments
Under the hood, the API executes a non-trivial SQL query: WITH latest_scheduled_rewards AS (
SELECT DISTINCT ON (participant_address) participant_address, scheduled_rewards
FROM daily_scheduled_rewards
ORDER BY participant_address, day DESC
)
SELECT
COALESCE(drt.to_address, lsr.participant_address) as participant_address,
COALESCE(SUM(drt.amount), 0) + COALESCE(lsr.scheduled_rewards, 0) as total_rewards
FROM daily_reward_transfers drt
FULL OUTER JOIN latest_scheduled_rewards lsr
ON drt.to_address = lsr.participant_address
WHERE (drt.day >= $1 AND drt.day <= $2) OR drt.day IS NULL
GROUP BY COALESCE(drt.to_address, lsr.participant_address), lsr.scheduled_rewards
ORDER BY total_rewards DESC Query plan printed by EXPLAIN ANALYZE:
The following index can speed up the query from CREATE INDEX daily_scheduled_rewards_participant_day ON daily_scheduled_rewards (participant_address, day DESC); However, that's not enough. I think we need to find a way how to optimise the first query that looks up the latest scheduled rewards for each participant, as that's taking most of the time. SELECT DISTINCT ON (participant_address) participant_address, scheduled_rewards
FROM daily_scheduled_rewards
ORDER BY participant_address, day DESC Further reading:
The following query seems to work faster (completes under ~700ms) and produce the same data (albeit unsorted, which saves ~400ms): WITH latest_scheduled_rewards_update AS (
SELECT participant_address, MAX(day) AS day
FROM daily_scheduled_rewards
GROUP BY participant_address
)
SELECT participant_address, scheduled_rewards
FROM latest_scheduled_rewards_update
LEFT JOIN daily_scheduled_rewards USING (participant_address, day); After adding another index: CREATE INDEX daily_scheduled_rewards_participant ON daily_scheduled_rewards (participant_address); The following query returns the data we need in about ~700ms. WITH
latest_scheduled_rewards_update AS (
SELECT participant_address, MAX(day) AS day
FROM daily_scheduled_rewards
GROUP BY participant_address
),
latest_scheduled_rewards AS (
SELECT participant_address, scheduled_rewards
FROM latest_scheduled_rewards_update
LEFT JOIN daily_scheduled_rewards USING (participant_address, day)
)
SELECT
COALESCE(drt.to_address, lsr.participant_address) as participant_address,
COALESCE(SUM(drt.amount), 0) + COALESCE(lsr.scheduled_rewards, 0) as total_rewards
FROM daily_reward_transfers drt
FULL OUTER JOIN latest_scheduled_rewards lsr
ON drt.to_address = lsr.participant_address
WHERE (drt.day >= '2024-08-01' AND drt.day <= '2024-10-01') OR drt.day IS NULL
GROUP BY COALESCE(drt.to_address, lsr.participant_address), lsr.scheduled_rewards
ORDER BY total_rewards DESC; I think this could be further improved by using |
Great work, Miro!! |
The panel "Leaderboard - FIL Earned" calls https://stats.filspark.com/participants/top-earning?from=2024-10-02&to=today, which takes several seconds to load (3-5 seconds).
Let's optimise this query to bring the query duration under 500ms.
The text was updated successfully, but these errors were encountered: