fix: removing VACUUM to speedup db operations

This commit is contained in:
Richard Ramos 2022-05-19 17:29:15 -04:00
parent b4697210b0
commit c3e4262475
No known key found for this signature in database
GPG Key ID: BD36D48BC9FFC88C
1 changed files with 8 additions and 6 deletions

View File

@ -113,28 +113,30 @@ func (d *DBStore) createTable() error {
} }
func (d *DBStore) cleanOlderRecords() error { func (d *DBStore) cleanOlderRecords() error {
d.log.Debug("Cleaning older records...")
// Delete older messages // Delete older messages
if d.maxDuration > 0 { if d.maxDuration > 0 {
start := time.Now()
sqlStmt := `DELETE FROM message WHERE receiverTimestamp < ?` sqlStmt := `DELETE FROM message WHERE receiverTimestamp < ?`
_, err := d.db.Exec(sqlStmt, utils.GetUnixEpochFrom(time.Now().Add(-d.maxDuration))) _, err := d.db.Exec(sqlStmt, utils.GetUnixEpochFrom(time.Now().Add(-d.maxDuration)))
if err != nil { if err != nil {
return err return err
} }
elapsed := time.Since(start)
d.log.Debug(fmt.Sprintf("Deleting older records from the DB took %s", elapsed))
} }
// Limit number of records to a max N // Limit number of records to a max N
if d.maxMessages > 0 { if d.maxMessages > 0 {
start := time.Now()
sqlStmt := `DELETE FROM message WHERE id IN (SELECT id FROM message ORDER BY receiverTimestamp DESC LIMIT -1 OFFSET ?)` sqlStmt := `DELETE FROM message WHERE id IN (SELECT id FROM message ORDER BY receiverTimestamp DESC LIMIT -1 OFFSET ?)`
_, err := d.db.Exec(sqlStmt, d.maxMessages) _, err := d.db.Exec(sqlStmt, d.maxMessages)
if err != nil { if err != nil {
return err return err
} }
} elapsed := time.Since(start)
d.log.Debug(fmt.Sprintf("Deleting excess records from the DB took %s", elapsed))
// reduce the size of the DB file after the delete operation. See: https://www.sqlite.org/lang_vacuum.html
_, err := d.db.Exec("VACUUM")
if err != nil {
return err
} }
return nil return nil