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 slow delete with onDelete: SetNull #19169

Closed
boenni23 opened this issue May 10, 2023 · 2 comments
Closed

MongoDB slow delete with onDelete: SetNull #19169

boenni23 opened this issue May 10, 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: indexes topic: mongodb topic: performance/queries topic: relationMode formerly `referentialIntegrity` topic: relations
Milestone

Comments

@boenni23
Copy link

boenni23 commented May 10, 2023

Bug description

When having a schema with a 1:n relation where you delete the left side. It takes on big collections (>1.Mio documents) long time to delete the document (>1 minute).

How to reproduce

You generate a bad query to check the depending documents

{"params":"[]","duration":12735,"query":"db.Broadcast.aggregate([ { $match: { $expr: { $and: [ { }, { $and: [ { $in: [ \"$productionId\", [ { $literal: ObjectId(\"5f913feb235d8600075e1797\"), }, ], ], }, { $ne: [ \"$productionId\", \"$$REMOVE\", ], }, ], }, ], }, }, }, { $project: { _id: 1, productionId: 1, }, }, ])"}

There need to be a query which can use the normal index in mongodb like
db.Broadcast.find({productionId: ObjectId(\"5f913feb235d8600075e1797\")})

Expected behavior

No response

Prisma information

model Broadcast {
  id String @id @default(auto()) @map("_id") @db.ObjectId

  productionId String?     @db.ObjectId
  production   Production? @relation("ProductionBroadcasts", fields: [productionId], references: [id], onDelete: SetNull, onUpdate: Cascade)
}

model Production {
  id               String                 @id @default(auto()) @map("_id") @db.ObjectId
}
prisma.production.delete({where: {id: "...."}})

Environment & setup

Node: 19.7.0
Mongo: 6.0.5

Prisma Version

PrismaClient: 4.14.0
@boenni23 boenni23 added the kind/bug A reported bug. label May 10, 2023
@boenni23
Copy link
Author

Probably related to #16916, #17142 and #15156

In all of these tickets you find unoptimized queries on mongo which cannot use the index.

If you query a relation you should rewrite the query.

@Jolg42 Jolg42 added bug/1-unconfirmed Bug should have enough information for reproduction, but confirmation has not happened yet. topic: indexes team/client Issue for team Client. topic: mongodb topic: performance/queries topic: relations labels May 10, 2023
@janpio janpio changed the title MongoDB slow delete with onDelete: SetNull MongoDB slow delete with onDelete: SetNull May 17, 2023
@janpio janpio added the topic: relationMode formerly `referentialIntegrity` label Apr 24, 2024
@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: indexes topic: mongodb topic: performance/queries topic: relationMode formerly `referentialIntegrity` topic: relations
Projects
None yet
Development

No branches or pull requests

4 participants