-
Notifications
You must be signed in to change notification settings - Fork 499
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
services/horizon/internal/db2/history: Optimize query for reaping lookup tables #5393
Conversation
Nice results! I have some other suggestions for things we could maybe test out to optimize further, but these shouldn't block merging this as is, as it's still a step forward.
to
|
I thought that multicolumn indexes could still be efficient as long as we use queries with equality constraints on the leading column (which is the case for our query on history_transaction_participants). Is that not the case? |
I think that's probably true to some degree, and maybe what they even purport in the docs. But indexes in general are really finicky. A multicolumn index is optimized for queries that utilize both columns in the index together in the same query (and in the exact same order of appearance in the query). As you said, since Unrelated to this, but another thought after reading the docs you linked on
|
@mollykarcher adding the index on the history_account_id column of the history_transaction_participants table proved to be extremely effective. The peak reaping duration reduced to just ~500 ms with the index: Before adding the index, the peak reaping duration was at 2 seconds: However, it took approximately 1.5 hours to add the index on staging (which has 1 year of history). But the biggest concern is that the index occupies 82 GB of disk space. Ironically the size of the history_accounts table on staging is 1612 MB (there are only ~4 million rows in history_accounts). It doesn't make sense to add an 82 GB index so we can reap a 1.6GB table more efficiently, so I have opted to forgo adding the history_account_id index. |
I have updated the PR to store the reap offsets in the horizon DB instead of in-memory. Thus allows reaping of lookup tables to resume from where it left off after horizon restarts |
PR Checklist
PR Structure
otherwise).
services/friendbot
, orall
ordoc
if the changes are broad or impact manypackages.
Thoroughness
.md
files, etc... affected by this change). Take a look in the
docs
folder for a given service,like this one.
Release planning
needed with deprecations, added features, breaking changes, and DB schema changes.
semver, or if it's mainly a patch change. The PR is targeted at the next
release branch if it's not a patch change.
What
Previously the sql queries to fined orphaned history lookup table rows looked like:
The idea behind this query is to iterate over rows from the
history_accounts
table in batches of 1000. For each row in the batch we check if the account id is referenced in any of the history tables. If there are no references then we know we have identified an orphaned row which can be deleted.Here is the explain analyze output on that query:
This PR changes the query to use LATERAL joins:
The new query is often faster because if it detects that an account id is referenced in one history table it aborts the search and does not query the remaining history tables in the subsequent lateral joins.
This change was deployed on staging on 7/18 at approximately 11am UTC. We can see that the duration of reaping lookup tables has decreased significantly:
We also observed a decrease in the number of timeout errors when reaping lookup tables:
Known limitations
[N/A]