-
Notifications
You must be signed in to change notification settings - Fork 529
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
batch insertion events in archive database #14779
Conversation
…MinaProtocol/mina into fix/archive-db-slowness
!ci-build-me |
in | ||
let increment_nonce = body.increment_nonce in | ||
let%bind events_id = | ||
Zkapp_events.add_if_doesn't_exist (module Conn) body.events | ||
Metrics.time ~label:"Zkapp_events.add" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do we need all of these Metrics.time
?
src/lib/mina_caqti/mina_caqti.ml
Outdated
@@ -287,15 +287,19 @@ let select_insert_into_cols ~(select : string * 'select Caqti_type.t) | |||
@@ select_cols ~select:(fst select) ~table_name ?tannot ~cols:(fst cols) () | |||
) | |||
value | |||
>>= function |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Previous code with >>= function
looked cleaner
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah. I changed it to add some more logs. Let me revert those changes.
src/lib/mina_caqti/mina_caqti.ml
Outdated
@@ insert_into_cols ~returning:(fst select) ~table_name ?tannot | ||
~cols:(fst cols) () ) | ||
value | ||
let%map res = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Redundant let%map
introduced
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Again this change was for logging. Let me undo that.
() | ||
>>| String.Map.of_alist_exn | ||
in | ||
let field_id_list_list = |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
let field_id_list_list = |
field_id_list_list
is not needed at all
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I added a let binding here to make the code more readable. Just to give a name to the result of that computation.
src/app/archive/lib/processor.ml
Outdated
WHERE element_ids in (%s) |sql} | ||
@@ sep_by_comma field_array_list | ||
in | ||
let module Field_array_map = Map.Make (struct |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Move this module to the module-level context (so that it's not defined in the middle of function definition)?
src/app/archive/lib/processor.ml
Outdated
let%bind field_id_list_list = | ||
if not @@ List.is_empty fields then | ||
let field_insert = | ||
sprintf |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you think we can generalize the pattern and extract it as a separate function to reduce code duplication?
I'm taking about insert on conflict do nothing
paired with select of some_field, id
with conversion of it to map preserving the initial order.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It should be possible to do that. In fact, Mina_caqti.select_insert_into_cols
did a similar abstraction over 2 queries. But for this one, it's a bit harder, because I was hand-written some of the query and didn't get to use caqti
's typ
stuff. Let me leave a comment around this part of code saying we could abstract this pattern out once I figure out how to use typ
to convert between values and sql representations. I think we want to merge this soon so let create a ticket for do that.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nevermind, I got most part of the refactor done. I left out the conversion from value to string.
!ci-build-me |
!ci-build-me |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Waiting for rampup8 release before landing this
!ci-build-me |
!ci-nightly-me |
!ci-build-me |
!ci-build-me |
!ci-nightly-me |
!ci-nightly-me |
!ci-nightly-me |
!ci-build-me |
!approved-for-mainnet |
Explain your changes:
This PR does batch insertion for
zkapp_events
in archive db.Since
Account_update.Body.Events'.t
is defined asfield array list
, which is ismorphic to a list of list of fields.We could batch the insertion of field and field_array to optimize the speed of archiving max-cost zkapps.
field_id
tofield
by querying against the zkapp_field tablelist of field_ids
in one queryfield_array_id
tofield_id array
by querying against the zkapp_field_array tableTo patch the existing database use
add_unique_constraints.sql
Explain how you tested your changes:
Tested locally and also on testworld-2-0
Checklist: