prisma / prisma-examples

🚀 Ready-to-run Prisma example projects

Home Page:https://www.prisma.io/docs/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

MongoDB indexes don't work with `prismagraphql/mongo-single-replica`

hffmnn opened this issue · comments

Hi.

I wanted to try out performance optimizations via indexes. As I wanted to start small, I used the example in databases/mongodb. In the folder I ran docker-compose --file docker-compose.yml up --detach followed by a

export DATABASE_URL="mongodb://root:prisma@localhost:27017/prisma-mongo?authSource=admin&retryWrites=true&w=majority"
yarn script:create-user

This worked as expected, the entry was in the collection prisma-mongo/User.

Then I added an index via the Mongodb client (yarn add mongodb@6.3) followed by a

const client = new MongoClient('mongodb://root:prisma@localhost:27017/prisma-mongo?authSource=admin&retryWrites=true&w=majority')
await client.connect()
const database = client.db('prisma-mongo')
const peaks = database.collection('User')
await peaks.createIndex({ firstName: 1 })

This script hangs forever and never completes.

When looking at the indexes via MongoDB Compass it looks like the index is created correctly though:
Screenshot 2024-02-07 at 08 55 18

But this index never gets used. Doing a simple query like { firstName: { $eg: "Hana"} }, explain gives the following:

{
  "queryPlanner": {
    "plannerVersion": 1,
    "namespace": "prisma-mongo.User",
    "indexFilterSet": false,
    "parsedQuery": {
      "firstName": { "$eq": "Hana" }
    },
    "winningPlan": {
      "stage": "COLLSCAN",
      "filter": {
        "firstName": { "$eq": "Hana" }
      },
      "direction": "forward"
    },
    "rejectedPlans": []
  },
  "executionStats": {
    "executionSuccess": true,
    "nReturned": 1,
    "executionTimeMillis": 0,
    "totalKeysExamined": 0,
    "totalDocsExamined": 1,
    "executionStages": {
      "stage": "COLLSCAN",
      "filter": {
        "firstName": { "$eq": "Hana" }
      },
      "nReturned": 1,
      "executionTimeMillisEstimate": 0,
      "works": 3,
      "advanced": 1,
      "needTime": 1,
      "needYield": 0,
      "saveState": 0,
      "restoreState": 0,
      "isEOF": 1,
      "direction": "forward",
      "docsExamined": 1
    },
    "allPlansExecution": []
  },
  "serverInfo": {
    "host": "323b689e1af8",
    "port": 27017,
    "version": "4.4.3",
    "gitVersion": "913d6b62acfbb344dde1b116f4161360acd8fd13"
  },
  "ok": 1,
  "$clusterTime": {
    "clusterTime": {
      "$timestamp": "7332765808688365569"
    },
    "signature": {
      "hash": "YjUc8VUByYLoiyVKDuFJkmgCTcQ=",
      "keyId": {
        "low": 4,
        "high": 1707291162,
        "unsigned": false
      }
    }
  },
  "operationTime": {
    "$timestamp": "7332765808688365569"
  }
}

So it uses a COLLSCAN instead of a IXSCAN.

When trying the same (The same means generating the index as described but generating the collection isn't done via prisma, because it needs a replica set.) against another mongo image (e.g.image: mongo:6-jammy) it works:

  • Creating the index doesn't hang but returns immediately
  • Doing the query gives IXSCAN

The only idea google came up with was this: https://stackoverflow.com/a/65699029 still I have no idea how to apply this.

Any ideas?