fix(reminders): replace N+1 per-appointment queries with single JOIN query
Replace the per-appointment sequential queries (client, pet, service, staff) in runReminderCheck with a single JOIN query that fetches all appointment data in one round-trip. Group results in memory using a Map for O(1) lookups. Before: 1 initial query + N*(1 existing + 4 related) = 1 + 5N queries After: 1 initial + 1 sent-check + 1 JOIN = 3 queries total Co-Authored-By: Paperclip <noreply@paperclip.ing>
This commit is contained in:
@@ -6,6 +6,7 @@ import {
|
|||||||
getDb,
|
getDb,
|
||||||
gte,
|
gte,
|
||||||
lt,
|
lt,
|
||||||
|
sql,
|
||||||
appointments,
|
appointments,
|
||||||
clients,
|
clients,
|
||||||
pets,
|
pets,
|
||||||
@@ -64,56 +65,68 @@ export async function runReminderCheck(): Promise<void> {
|
|||||||
)
|
)
|
||||||
);
|
);
|
||||||
|
|
||||||
for (const appt of upcoming) {
|
const appointmentIds: string[] = upcoming.map((a) => a.id as string);
|
||||||
// Check if reminder already sent (unique constraint prevents double-send)
|
|
||||||
const existing = await db
|
if (appointmentIds.length === 0) continue;
|
||||||
.select({ id: reminderLogs.id })
|
|
||||||
.from(reminderLogs)
|
const sentAppointmentIds = new Set(
|
||||||
.where(
|
(
|
||||||
and(
|
await db
|
||||||
eq(reminderLogs.appointmentId, appt.id),
|
.select({ appointmentId: reminderLogs.appointmentId })
|
||||||
eq(reminderLogs.reminderType, window.label)
|
.from(reminderLogs)
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
eq(reminderLogs.reminderType, window.label),
|
||||||
|
appointmentIds.length === 1
|
||||||
|
? eq(reminderLogs.appointmentId, appointmentIds[0]!)
|
||||||
|
: sql`${reminderLogs.appointmentId} = ANY(${appointmentIds})`
|
||||||
|
)
|
||||||
)
|
)
|
||||||
|
).map((r) => r.appointmentId)
|
||||||
|
);
|
||||||
|
|
||||||
|
const joinedRows = await db
|
||||||
|
.select({
|
||||||
|
appointmentId: appointments.id,
|
||||||
|
startTime: appointments.startTime,
|
||||||
|
clientId: appointments.clientId,
|
||||||
|
petId: appointments.petId,
|
||||||
|
serviceId: appointments.serviceId,
|
||||||
|
staffId: appointments.staffId,
|
||||||
|
confirmationToken: appointments.confirmationToken,
|
||||||
|
clientName: clients.name,
|
||||||
|
clientEmail: clients.email,
|
||||||
|
clientEmailOptOut: clients.emailOptOut,
|
||||||
|
petName: pets.name,
|
||||||
|
serviceName: services.name,
|
||||||
|
staffName: staff.name,
|
||||||
|
})
|
||||||
|
.from(appointments)
|
||||||
|
.innerJoin(clients, eq(appointments.clientId, clients.id))
|
||||||
|
.innerJoin(pets, eq(appointments.petId, pets.id))
|
||||||
|
.innerJoin(services, eq(appointments.serviceId, services.id))
|
||||||
|
.leftJoin(staff, eq(appointments.staffId, staff.id))
|
||||||
|
.where(
|
||||||
|
and(
|
||||||
|
gte(appointments.startTime, windowStart),
|
||||||
|
lt(appointments.startTime, windowEnd),
|
||||||
|
eq(appointments.status, "scheduled")
|
||||||
)
|
)
|
||||||
.limit(1);
|
);
|
||||||
|
|
||||||
if (existing.length > 0) continue; // already sent
|
const appointmentMap = new Map<string, typeof joinedRows[number]>();
|
||||||
|
for (const row of joinedRows) {
|
||||||
|
appointmentMap.set(row.appointmentId, row);
|
||||||
|
}
|
||||||
|
|
||||||
// Fetch related records for the email
|
for (const appt of upcoming) {
|
||||||
const [client] = await db
|
if (sentAppointmentIds.has(appt.id)) continue;
|
||||||
.select({ name: clients.name, email: clients.email, emailOptOut: clients.emailOptOut })
|
|
||||||
.from(clients)
|
|
||||||
.where(eq(clients.id, appt.clientId))
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
if (!client || !client.email || client.emailOptOut) continue;
|
const row = appointmentMap.get(appt.id);
|
||||||
|
if (!row) continue;
|
||||||
|
if (!row.clientEmail || row.clientEmailOptOut) continue;
|
||||||
|
if (!row.petName || !row.serviceName) continue;
|
||||||
|
|
||||||
const [pet] = await db
|
|
||||||
.select({ name: pets.name })
|
|
||||||
.from(pets)
|
|
||||||
.where(eq(pets.id, appt.petId))
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
const [service] = await db
|
|
||||||
.select({ name: services.name })
|
|
||||||
.from(services)
|
|
||||||
.where(eq(services.id, appt.serviceId))
|
|
||||||
.limit(1);
|
|
||||||
|
|
||||||
let groomerName: string | null = null;
|
|
||||||
if (appt.staffId) {
|
|
||||||
const [groomer] = await db
|
|
||||||
.select({ name: staff.name })
|
|
||||||
.from(staff)
|
|
||||||
.where(eq(staff.id, appt.staffId))
|
|
||||||
.limit(1);
|
|
||||||
groomerName = groomer?.name ?? null;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (!pet || !service) continue;
|
|
||||||
|
|
||||||
// Ensure the appointment has a confirmation token before sending the reminder.
|
|
||||||
// Generate one if it doesn't have one yet (e.g. pre-existing appointments).
|
|
||||||
let confirmationToken = appt.confirmationToken;
|
let confirmationToken = appt.confirmationToken;
|
||||||
if (!confirmationToken) {
|
if (!confirmationToken) {
|
||||||
confirmationToken = randomBytes(32).toString("hex");
|
confirmationToken = randomBytes(32).toString("hex");
|
||||||
@@ -125,12 +138,12 @@ export async function runReminderCheck(): Promise<void> {
|
|||||||
|
|
||||||
const sent = await sendEmail(
|
const sent = await sendEmail(
|
||||||
buildReminderEmail(
|
buildReminderEmail(
|
||||||
client.email,
|
row.clientEmail,
|
||||||
{
|
{
|
||||||
clientName: client.name,
|
clientName: row.clientName,
|
||||||
petName: pet.name,
|
petName: row.petName,
|
||||||
serviceName: service.name,
|
serviceName: row.serviceName,
|
||||||
groomerName,
|
groomerName: row.staffName ?? null,
|
||||||
startTime: appt.startTime,
|
startTime: appt.startTime,
|
||||||
},
|
},
|
||||||
window.hours,
|
window.hours,
|
||||||
@@ -139,7 +152,6 @@ export async function runReminderCheck(): Promise<void> {
|
|||||||
);
|
);
|
||||||
|
|
||||||
if (sent) {
|
if (sent) {
|
||||||
// Record send — ignore conflicts (race condition between instances)
|
|
||||||
await db
|
await db
|
||||||
.insert(reminderLogs)
|
.insert(reminderLogs)
|
||||||
.values({ appointmentId: appt.id, reminderType: window.label })
|
.values({ appointmentId: appt.id, reminderType: window.label })
|
||||||
|
|||||||
Reference in New Issue
Block a user