Use prepared statement for bulk writes

When executing the same statement multiple times, avoid
the overhead of re-parsing the statement for each insert.
This commit is contained in:
Nick Farrell 2022-12-12 13:39:02 +11:00
parent 321ed12663
commit 1235ea5bb5
No known key found for this signature in database
GPG key ID: 740D3A86CF435835

View file

@ -258,12 +258,23 @@ func (c *messageCache) addMessages(ms []*message) error {
if c.nop {
return nil
}
if len(ms) == 0 {
return nil
}
start := time.Now()
tx, err := c.db.Begin()
if err != nil {
return err
}
defer tx.Rollback()
statement, err := tx.Prepare(
insertMessageQuery,
)
if err != nil {
return err
}
defer statement.Close()
for _, m := range ms {
if m.Event != messageEvent {
return errUnexpectedMessageType
@ -291,8 +302,7 @@ func (c *messageCache) addMessages(ms []*message) error {
if m.Sender.IsValid() {
sender = m.Sender.String()
}
_, err := tx.Exec(
insertMessageQuery,
_, err := statement.Exec(
m.ID,
m.Time,
m.Topic,