From 7f23492d5379913878e14a4f41add2b5c3e5fba5 Mon Sep 17 00:00:00 2001 From: hpkoh Date: Wed, 26 Oct 2022 00:12:13 +0800 Subject: [PATCH] [questions][feat] parallelize queries --- .../migration.sql | 8 ++ .../migrations/20221025014050_/migration.sql | 8 ++ .../questions-question-encounter-router.ts | 75 ++++++++++--------- 3 files changed, 56 insertions(+), 35 deletions(-) create mode 100644 apps/portal/prisma/migrations/20221025013857_add_full_text_search/migration.sql create mode 100644 apps/portal/prisma/migrations/20221025014050_/migration.sql diff --git a/apps/portal/prisma/migrations/20221025013857_add_full_text_search/migration.sql b/apps/portal/prisma/migrations/20221025013857_add_full_text_search/migration.sql new file mode 100644 index 00000000..8f58d7f9 --- /dev/null +++ b/apps/portal/prisma/migrations/20221025013857_add_full_text_search/migration.sql @@ -0,0 +1,8 @@ +-- AlterTable +ALTER TABLE "QuestionsQuestion" ADD COLUMN "contentSearch" TSVECTOR + GENERATED ALWAYS AS + (to_tsvector('english', coalesce(content, ''))) + STORED; + +-- CreateIndex +CREATE INDEX "QuestionsQuestion_contentSearch_idx" ON "QuestionsQuestion" USING GIN("contentSearch"); diff --git a/apps/portal/prisma/migrations/20221025014050_/migration.sql b/apps/portal/prisma/migrations/20221025014050_/migration.sql new file mode 100644 index 00000000..6d3a3407 --- /dev/null +++ b/apps/portal/prisma/migrations/20221025014050_/migration.sql @@ -0,0 +1,8 @@ +-- DropIndex +DROP INDEX "QuestionsQuestion_contentSearch_idx"; + +-- AlterTable +ALTER TABLE "QuestionsQuestion" ALTER COLUMN "contentSearch" DROP DEFAULT; + +-- CreateIndex +CREATE INDEX "QuestionsQuestion_contentSearch_idx" ON "QuestionsQuestion"("contentSearch"); diff --git a/apps/portal/src/server/router/questions-question-encounter-router.ts b/apps/portal/src/server/router/questions-question-encounter-router.ts index f43578e8..1a55247e 100644 --- a/apps/portal/src/server/router/questions-question-encounter-router.ts +++ b/apps/portal/src/server/router/questions-question-encounter-router.ts @@ -71,18 +71,20 @@ export const questionsQuestionEncounterRouter = createProtectedRouter() const userId = ctx.session?.user?.id; return await ctx.prisma.$transaction(async (tx) => { - const questionToUpdate = await tx.questionsQuestion.findUnique({ - where: { - id: input.questionId, - }, - }); + const [questionToUpdate, questionEncounterCreated] = await Promise.all([ + tx.questionsQuestion.findUnique({ + where: { + id: input.questionId, + }, + }), + tx.questionsQuestionEncounter.create({ + data: { + ...input, + userId, + }, + }) + ]); - const questionEncounterCreated = await tx.questionsQuestionEncounter.create({ - data: { - ...input, - userId, - }, - }); if (questionToUpdate === null) { throw new TRPCError({ @@ -131,20 +133,22 @@ export const questionsQuestionEncounterRouter = createProtectedRouter() } return await ctx.prisma.$transaction(async (tx) => { - const questionToUpdate = await tx.questionsQuestion.findUnique({ - where: { - id: questionEncounterToUpdate.questionId, - }, - }); + const [questionToUpdate, questionEncounterUpdated] = await Promise.all([ + tx.questionsQuestion.findUnique({ + where: { + id: questionEncounterToUpdate.questionId, + }, + }), + tx.prisma.questionsQuestionEncounter.update({ + data: { + ...input, + }, + where: { + id: input.id, + }, + }) + ]); - const questionEncounterUpdated = await ctx.prisma.questionsQuestionEncounter.update({ - data: { - ...input, - }, - where: { - id: input.id, - }, - }); if (questionToUpdate!.lastSeenAt === questionEncounterToUpdate.seenAt) { const latestEncounter = await ctx.prisma.questionsQuestionEncounter.findFirst({ @@ -194,17 +198,18 @@ export const questionsQuestionEncounterRouter = createProtectedRouter() } return await ctx.prisma.$transaction(async (tx) => { - const questionToUpdate = await tx.questionsQuestion.findUnique({ - where: { - id: questionEncounterToDelete.questionId, - }, - }); - - const questionEncounterDeleted = await ctx.prisma.questionsQuestionEncounter.delete({ - where: { - id: input.id, - }, - }); + const [questionToUpdate, questionEncounterDeleted] = await Promise.all([ + tx.questionsQuestion.findUnique({ + where: { + id: questionEncounterToDelete.questionId, + }, + }), + tx.prisma.questionsQuestionEncounter.delete({ + where: { + id: input.id, + }, + }) + ]); if (questionToUpdate!.lastSeenAt === questionEncounterToDelete.seenAt) { const latestEncounter = await ctx.prisma.questionsQuestionEncounter.findFirst({