Improve performance by placing updates in transaction

This commit is contained in:
Noi 2021-12-05 18:29:27 -08:00
parent 51e241aca8
commit 3741222c68

View file

@ -61,6 +61,7 @@ class DataRetention : BackgroundService {
// Do actual updates // Do actual updates
int updatedGuilds = 0; int updatedGuilds = 0;
int updatedUsers = 0; int updatedUsers = 0;
using (var tUpdate = db.BeginTransaction()) {
foreach (var item in updateList) { foreach (var item in updateList) {
var guild = item.Key; var guild = item.Key;
var userlist = item.Value; var userlist = item.Value;
@ -74,13 +75,15 @@ class DataRetention : BackgroundService {
updatedUsers += await cUpdateGuildUser.ExecuteNonQueryAsync(CancellationToken.None).ConfigureAwait(false); updatedUsers += await cUpdateGuildUser.ExecuteNonQueryAsync(CancellationToken.None).ConfigureAwait(false);
} }
} }
await tUpdate.CommitAsync(CancellationToken.None).ConfigureAwait(false);
}
var resultText = new StringBuilder(); var resultText = new StringBuilder();
resultText.Append($"Updated {updatedGuilds} guilds, {updatedUsers} users."); resultText.Append($"Updated {updatedGuilds} guilds, {updatedUsers} users.");
// Deletes both guild and user data if it hasn't been seen for over the threshold defined at the top of this file // Deletes both guild and user data if it hasn't been seen for over the threshold defined at the top of this file
// Expects referencing tables to have 'on delete cascade' // Expects referencing tables to have 'on delete cascade'
using var t = db.BeginTransaction();
int staleGuilds, staleUsers; int staleGuilds, staleUsers;
using (var tRemove = db.BeginTransaction()) {
using (var c = db.CreateCommand()) { using (var c = db.CreateCommand()) {
c.CommandText = $"delete from {GuildConfiguration.BackingTable}" + c.CommandText = $"delete from {GuildConfiguration.BackingTable}" +
$" where (now() - interval '{StaleGuildThreshold} days') > last_seen"; $" where (now() - interval '{StaleGuildThreshold} days') > last_seen";
@ -91,6 +94,8 @@ class DataRetention : BackgroundService {
$" where (now() - interval '{StaleUserThreashold} days') > last_seen"; $" where (now() - interval '{StaleUserThreashold} days') > last_seen";
staleUsers = await c.ExecuteNonQueryAsync(CancellationToken.None).ConfigureAwait(false); staleUsers = await c.ExecuteNonQueryAsync(CancellationToken.None).ConfigureAwait(false);
} }
await tRemove.CommitAsync(CancellationToken.None).ConfigureAwait(false);
}
if (staleGuilds != 0 || staleUsers != 0) { if (staleGuilds != 0 || staleUsers != 0) {
resultText.Append(" Discarded "); resultText.Append(" Discarded ");
if (staleGuilds != 0) { if (staleGuilds != 0) {
@ -102,7 +107,6 @@ class DataRetention : BackgroundService {
} }
resultText.Append('.'); resultText.Append('.');
} }
t.Commit();
Log(resultText.ToString()); Log(resultText.ToString());
} finally { } finally {
_updateLock.Release(); _updateLock.Release();