Skip to content

Commit b377b57

Browse files
authored
db efficiency improvements (#1657)
- remove calls to findUnique, especially when including/selecting relations - add some missing indexes - add spans to $transaction calls to help track down long running txs
1 parent b67ea9a commit b377b57

File tree

53 files changed

+237
-160
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

53 files changed

+237
-160
lines changed

apps/webapp/app/db.server.ts

Lines changed: 67 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ import { logger } from "./services/logger.server";
1313
import { isValidDatabaseUrl } from "./utils/db";
1414
import { singleton } from "./utils/singleton";
1515
import { $transaction as transac } from "@trigger.dev/database";
16+
import { startActiveSpan } from "./v3/tracer.server";
17+
import { Span } from "@opentelemetry/api";
1618

1719
export type {
1820
PrismaTransactionClient,
@@ -21,25 +23,76 @@ export type {
2123
PrismaReplicaClient,
2224
};
2325

26+
export async function $transaction<R>(
27+
prisma: PrismaClientOrTransaction,
28+
name: string,
29+
fn: (prisma: PrismaTransactionClient, span?: Span) => Promise<R>,
30+
options?: PrismaTransactionOptions
31+
): Promise<R | undefined>;
2432
export async function $transaction<R>(
2533
prisma: PrismaClientOrTransaction,
2634
fn: (prisma: PrismaTransactionClient) => Promise<R>,
2735
options?: PrismaTransactionOptions
36+
): Promise<R | undefined>;
37+
export async function $transaction<R>(
38+
prisma: PrismaClientOrTransaction,
39+
fnOrName: ((prisma: PrismaTransactionClient) => Promise<R>) | string,
40+
fnOrOptions?: ((prisma: PrismaTransactionClient) => Promise<R>) | PrismaTransactionOptions,
41+
options?: PrismaTransactionOptions
2842
): Promise<R | undefined> {
29-
return transac(
30-
prisma,
31-
fn,
32-
(error) => {
33-
logger.error("prisma.$transaction error", {
34-
code: error.code,
35-
meta: error.meta,
36-
stack: error.stack,
37-
message: error.message,
38-
name: error.name,
39-
});
40-
},
41-
options
42-
);
43+
if (typeof fnOrName === "string") {
44+
return await startActiveSpan(fnOrName, async (span) => {
45+
span.setAttribute("$transaction", true);
46+
47+
if (options?.isolationLevel) {
48+
span.setAttribute("isolation_level", options.isolationLevel);
49+
}
50+
51+
if (options?.timeout) {
52+
span.setAttribute("timeout", options.timeout);
53+
}
54+
55+
if (options?.maxWait) {
56+
span.setAttribute("max_wait", options.maxWait);
57+
}
58+
59+
if (options?.swallowPrismaErrors) {
60+
span.setAttribute("swallow_prisma_errors", options.swallowPrismaErrors);
61+
}
62+
63+
const fn = fnOrOptions as (prisma: PrismaTransactionClient, span: Span) => Promise<R>;
64+
65+
return transac(
66+
prisma,
67+
(client) => fn(client, span),
68+
(error) => {
69+
logger.error("prisma.$transaction error", {
70+
code: error.code,
71+
meta: error.meta,
72+
stack: error.stack,
73+
message: error.message,
74+
name: error.name,
75+
});
76+
},
77+
options
78+
);
79+
});
80+
} else {
81+
return transac(
82+
prisma,
83+
fnOrName,
84+
(error) => {
85+
logger.error("prisma.$transaction error", {
86+
code: error.code,
87+
meta: error.meta,
88+
stack: error.stack,
89+
message: error.message,
90+
name: error.name,
91+
});
92+
},
93+
typeof fnOrOptions === "function" ? undefined : fnOrOptions
94+
);
95+
}
4396
}
4497

4598
export { Prisma };

apps/webapp/app/presenters/v3/ApiBatchResultsPresenter.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class ApiBatchResultsPresenter extends BasePresenter {
99
env: AuthenticatedEnvironment
1010
): Promise<BatchTaskRunExecutionResult | undefined> {
1111
return this.traceWithEnv("call", env, async (span) => {
12-
const batchRun = await this._prisma.batchTaskRun.findUnique({
12+
const batchRun = await this._prisma.batchTaskRun.findFirst({
1313
where: {
1414
friendlyId,
1515
runtimeEnvironmentId: env.id,

apps/webapp/app/presenters/v3/ApiRunResultPresenter.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ export class ApiRunResultPresenter extends BasePresenter {
99
env: AuthenticatedEnvironment
1010
): Promise<TaskRunExecutionResult | undefined> {
1111
return this.traceWithEnv("call", env, async (span) => {
12-
const taskRun = await this._prisma.taskRun.findUnique({
12+
const taskRun = await this._prisma.taskRun.findFirst({
1313
where: {
1414
friendlyId,
1515
runtimeEnvironmentId: env.id,

apps/webapp/app/presenters/v3/DeploymentPresenter.server.ts

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -53,12 +53,10 @@ export class DeploymentPresenter {
5353
},
5454
});
5555

56-
const deployment = await this.#prismaClient.workerDeployment.findUniqueOrThrow({
56+
const deployment = await this.#prismaClient.workerDeployment.findFirstOrThrow({
5757
where: {
58-
projectId_shortCode: {
59-
projectId: project.id,
60-
shortCode: deploymentShortCode,
61-
},
58+
projectId: project.id,
59+
shortCode: deploymentShortCode,
6260
},
6361
select: {
6462
id: true,

apps/webapp/app/presenters/v3/EnvironmentVariablesPresenter.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export class EnvironmentVariablesPresenter {
1515
}
1616

1717
public async call({ userId, projectSlug }: { userId: User["id"]; projectSlug: Project["slug"] }) {
18-
const project = await this.#prismaClient.project.findUnique({
18+
const project = await this.#prismaClient.project.findFirst({
1919
select: {
2020
id: true,
2121
},

apps/webapp/app/presenters/v3/NewAlertChannelPresenter.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ import { WebClient } from "@slack/web-api";
88

99
export class NewAlertChannelPresenter extends BasePresenter {
1010
public async call(projectId: string) {
11-
const project = await this._prisma.project.findUniqueOrThrow({
11+
const project = await this._prisma.project.findFirstOrThrow({
1212
where: {
1313
id: projectId,
1414
},

apps/webapp/app/presenters/v3/RunListPresenter.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,7 +131,7 @@ export class RunListPresenter extends BasePresenter {
131131

132132
//bulk id
133133
if (bulkId) {
134-
const bulkAction = await this._replica.bulkActionGroup.findUnique({
134+
const bulkAction = await this._replica.bulkActionGroup.findFirst({
135135
select: {
136136
items: {
137137
select: {

apps/webapp/app/presenters/v3/RunStreamPresenter.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ export class RunStreamPresenter {
2121
request: Request;
2222
runFriendlyId: TaskRun["friendlyId"];
2323
}) {
24-
const run = await this.#prismaClient.taskRun.findUnique({
24+
const run = await this.#prismaClient.taskRun.findFirst({
2525
where: {
2626
friendlyId: runFriendlyId,
2727
},

apps/webapp/app/presenters/v3/SpanPresenter.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ export class SpanPresenter extends BasePresenter {
2929
spanId: string;
3030
runFriendlyId: string;
3131
}) {
32-
const project = await this._replica.project.findUnique({
32+
const project = await this._replica.project.findFirst({
3333
where: {
3434
slug: projectSlug,
3535
},

apps/webapp/app/presenters/v3/TasksStreamPresenter.server.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ export class TasksStreamPresenter {
3333
projectSlug: string;
3434
userId: string;
3535
}) {
36-
const project = await this.#prismaClient.project.findUnique({
36+
const project = await this.#prismaClient.project.findFirst({
3737
where: {
3838
slug: projectSlug,
3939
organization: {

0 commit comments

Comments
 (0)