Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongodb Nested Queries Not Using Indexes #18025

Closed
peymil opened this issue Feb 21, 2023 · 2 comments
Closed

Mongodb Nested Queries Not Using Indexes #18025

peymil opened this issue Feb 21, 2023 · 2 comments
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. topic: include topic: indexes topic: mongodb topic: performance/queries topic: performance
Milestone

Comments

@peymil
Copy link

peymil commented Feb 21, 2023

Bug description

In my app, querying orders and their subtitle works takes around 22 secs (33908 orders, 25534 work). I reproduced my issue with a small script and schema file.

Also index usage count is not increasing in second query.

I'm currently using a local single node mongoldb replica set for reproducing, also tried in atlas replica set instance.

How to reproduce

import { PrismaClient } from '@prisma/client';

const prismaClient = new PrismaClient({
  datasources: {
    db: { url: 'mongourl' },
  },
  log: ['query', 'info', 'warn', 'error'],
});

console.time('work findmany');

await prismaClient.subtitleWork.findFirst({
  where: {
    orderId: '60b3e0be7a8dc3424dfd1afc',
  },
});

console.timeEnd('work findmany');

console.time('order findunique');

const order = await prismaClient.order.findUniqueOrThrow({
  where: {
    id: '5edf8699f51916565a5eac8a',
  },
  include: {
    subtitleWork: true,
  },
});
console.timeEnd('order findunique');

Expected behavior

Including subtitleWork must use index of orderId.

Prisma information

datasource db {
  provider = "mongodb"
  url      = env("DATABASE_URL")
}

model SubtitleWork {
  id                   String        @id @default(auto()) @map("_id") @db.ObjectId
  createdAt            DateTime      @default(now())
  updatedAt            DateTime      @default(now()) @updatedAt
  ...
  order                Order         @relation(fields: [orderId], references: [id])
  orderId              String        @unique @db.ObjectId
}

model Order {
  id                           String            @id @default(auto()) @map("_id") @db.ObjectId
  createdAt                    DateTime          @default(now())
  updatedAt                    DateTime          @default(now()) @updatedAt
  createdBy                    String            @db.ObjectId
  updatedBy                    String            @db.ObjectId
  ...
  subtitleWork                 SubtitleWork?
}

Environment & setup

  • OS: macOS
  • Database: MongoDB
  • Node.js version: v18.12.1

Prisma Version

 4.10.1
@peymil peymil added the kind/bug A reported bug. label Feb 21, 2023
@peymil
Copy link
Author

peymil commented Feb 22, 2023

Duplicate of #17396

@janpio janpio added topic: indexes topic: mongodb bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. team/client Issue for team Client. topic: performance labels Mar 2, 2023
@laplab
Copy link
Contributor

laplab commented May 13, 2024

This performance problem is fixed in Prisma 5.14.0

@laplab laplab closed this as completed May 13, 2024
@laplab laplab added this to the 5.14.0 milestone May 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. kind/bug A reported bug. team/client Issue for team Client. topic: include topic: indexes topic: mongodb topic: performance/queries topic: performance
Projects
None yet
Development

No branches or pull requests

3 participants