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,35 +61,40 @@ class DataRetention : BackgroundService {
// Do actual updates // Do actual updates
int updatedGuilds = 0; int updatedGuilds = 0;
int updatedUsers = 0; int updatedUsers = 0;
foreach (var item in updateList) { using (var tUpdate = db.BeginTransaction()) {
var guild = item.Key; foreach (var item in updateList) {
var userlist = item.Value; var guild = item.Key;
var userlist = item.Value;
pUpdateG.Value = (long)guild; pUpdateG.Value = (long)guild;
updatedGuilds += await cUpdateGuild.ExecuteNonQueryAsync(CancellationToken.None).ConfigureAwait(false); updatedGuilds += await cUpdateGuild.ExecuteNonQueryAsync(CancellationToken.None).ConfigureAwait(false);
pUpdateGU_g.Value = (long)guild; pUpdateGU_g.Value = (long)guild;
foreach (var userid in userlist) { foreach (var userid in userlist) {
pUpdateGU_u.Value = (long)userid; pUpdateGU_u.Value = (long)userid;
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 c = db.CreateCommand()) { using (var tRemove = db.BeginTransaction()) {
c.CommandText = $"delete from {GuildConfiguration.BackingTable}" + using (var c = db.CreateCommand()) {
$" where (now() - interval '{StaleGuildThreshold} days') > last_seen"; c.CommandText = $"delete from {GuildConfiguration.BackingTable}" +
staleGuilds = await c.ExecuteNonQueryAsync(CancellationToken.None).ConfigureAwait(false); $" where (now() - interval '{StaleGuildThreshold} days') > last_seen";
} staleGuilds = await c.ExecuteNonQueryAsync(CancellationToken.None).ConfigureAwait(false);
using (var c = db.CreateCommand()) { }
c.CommandText = $"delete from {GuildUserConfiguration.BackingTable}" + using (var c = db.CreateCommand()) {
$" where (now() - interval '{StaleUserThreashold} days') > last_seen"; c.CommandText = $"delete from {GuildUserConfiguration.BackingTable}" +
staleUsers = await c.ExecuteNonQueryAsync(CancellationToken.None).ConfigureAwait(false); $" where (now() - interval '{StaleUserThreashold} days') > last_seen";
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 ");
@ -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();