Remove forUpdate() call in stats

This commit is contained in:
Chad Curtis 2025-09-15 08:01:47 +00:00
parent 370deac1af
commit b88f532e4c

View file

@ -302,23 +302,26 @@ export async function updateAuthorStats(
search: '', search: '',
}; };
// Try to update first - this is more efficient under high concurrency
const prev = await kysely const prev = await kysely
.selectFrom('author_stats') .selectFrom('author_stats')
.selectAll() .selectAll()
.forUpdate()
.where('pubkey', '=', pubkey) .where('pubkey', '=', pubkey)
.executeTakeFirst(); .executeTakeFirst();
const stats = fn(prev ?? empty);
if (prev) { if (prev) {
// Row exists, update it
const stats = fn(prev);
await kysely.updateTable('author_stats') await kysely.updateTable('author_stats')
.set(stats) .set(stats)
.where('pubkey', '=', pubkey) .where('pubkey', '=', pubkey)
.execute(); .execute();
} else { } else {
// Row doesn't exist, insert it
const stats = fn(empty);
await kysely.insertInto('author_stats') await kysely.insertInto('author_stats')
.values({ ...empty, ...stats }) .values({ ...empty, ...stats })
.onConflict((oc) => oc.column('pubkey').doUpdateSet(stats))
.execute(); .execute();
} }
} }
@ -352,23 +355,26 @@ export async function updateEventStats(
reactions: '{}', reactions: '{}',
}; };
// Try to update first - this is more efficient under high concurrency
const prev = await kysely const prev = await kysely
.selectFrom('event_stats') .selectFrom('event_stats')
.selectAll() .selectAll()
.forUpdate()
.where('event_id', '=', eventId) .where('event_id', '=', eventId)
.executeTakeFirst(); .executeTakeFirst();
const stats = fn(prev ?? empty);
if (prev) { if (prev) {
// Row exists, update it
const stats = fn(prev);
await kysely.updateTable('event_stats') await kysely.updateTable('event_stats')
.set(stats) .set(stats)
.where('event_id', '=', eventId) .where('event_id', '=', eventId)
.execute(); .execute();
} else { } else {
// Row doesn't exist, insert it
const stats = fn(empty);
await kysely.insertInto('event_stats') await kysely.insertInto('event_stats')
.values({ ...empty, ...stats }) .values({ ...empty, ...stats })
.onConflict((oc) => oc.column('event_id').doUpdateSet(stats))
.execute(); .execute();
} }
} }