PrismaP
Prisma8mo ago
8 replies
leofc

Prisma performance questions

Let's say I have this schema:

model Tenant {
  id String @id @default(cuid())
  name String

  customers Customer[]
  users User[]
  salesOrders SalesOrder[]
}

model Customer {
  id String @id @default(cuid())
  name String
  tenantId String
  tenant Tenant @relation(fields: [tenantId], references: [id])

  salesOrders SalesOrder[]
}

model User {
  id String @id @default(cuid())
  name String
  tenantId String
  tenant Tenant @relation(fields: [tenantId], references: [id])

  salesOrders SalesOrder[]
}

model SalesOrder {
  id String @id @default(cuid())
  customerId String
  customer Customer @relation(fields: [customerId], references: [id])
  userId String
  user User @relation(fields: [userId], references: [id])

  tenantId String
  tenant Tenant @relation(fields: [tenantId], references: [id])
}


I want to create a query that gets all the sales orders for a given tenant.

const salesOrders = await prisma.salesOrder.findMany({
  include: {
    customer: true,
    user: true,
  },
  where: {
    tenantId: tenant.id,
  },
});


For context, this is a multi-tenant application sharing the same database. Looking at some slowness, I assume this would be better, correct?

const salesOrders = await prisma.salesOrder.findMany({
  include: {
    customer: true,
    user: true,
  },
  where: {
    tenantId: tenant.id,
    customer: {
      tenantId: tenant.id,
    },
    user: {
      tenantId: tenant.id,
    },
  },
});


Another thing we don't have is indexes on the tenantId fields. I assume this would also help, correct?

What other things could we do to improve the performance of this query?
Was this page helpful?