Skip to content
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

analyzer/block: more efficient db query on startup #559

Merged
merged 4 commits into from
Nov 8, 2023

Conversation

mitjat
Copy link
Contributor

@mitjat mitjat commented Nov 7, 2023

Task

faster startup: More efficient softEnqueueGapsInProcessedBlocks()

This fn is a bottleneck during startup. It performs a very expensive SQL query. Offline experiments show that a SELECT FROM range(...) EXCEPT SELECT ... FROM analysis.processed_blocks would be much faster.

This PR

This PR speeds up a query that runs on every startup. Tested on current staging testnet (~20M rows in table): The old query takes 77s, the new query takes ~32s. Still slow, but at least better.

The PR also adds a unit test for the query, and emits a log line before running the expensive query so that logs make more sense at nexus startup time when the program apparently freezes for a bit.

Copy link
Collaborator

@pro-wh pro-wh left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sql wiz

gaps AS (
SELECT h
FROM highest_encountered_block, generate_series(GREATEST(1, $2::bigint), LEAST(highest_encountered_block.height, $3::bigint)) AS h
EXCEPT
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ℹ️ EXCEPT them, whereas previously it fully relied on ON CONFLICT DO NOTHING

@mitjat mitjat force-pushed the mitjat/softEnqueueGapsInProcessedBlocks branch from 251c457 to ea2fa15 Compare November 8, 2023 01:49
@mitjat mitjat enabled auto-merge November 8, 2023 01:50
@mitjat mitjat merged commit 2a0d246 into main Nov 8, 2023
6 checks passed
@mitjat mitjat deleted the mitjat/softEnqueueGapsInProcessedBlocks branch November 8, 2023 05:15
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants