Skip to content

Performance issues after 2.14 #5792

Closed
Closed

Description

@GCastilho

Bug description

After upgrading prisma do a version higher than 2.14 I'm experiencing performance issues with the queries. Mocha reports tests that usually took less than 60ms to now be taking more than 150ms, sometimes more than 200ms to complete

I commented about this in #5485 and analyzed the logs, but they're identical (or very similar) so it does not seems like it's the same as #5274

Expected behavior

Average performance should remain the same or improve between Prisma versions.

Prisma information

I've picked some queries to show comparing 2.14 with 2.15 and 2.17, but they were exactly the same, the only difference was the time it took to run them (54ms -> 141ms, 65ms -> 193ms) so I don't thing it make sense to show them (since they're the same). But it's not a problem to share that part of the schema and queries if needed

Environment & setup

  • OS: Debian 10 buster x64
  • Database: PostgreSQL 12 (localhost)
  • Node.js version: v14.15.5
  • Prisma version: >= 2.15.0

Please let me know if you need more information or tests that you need. Thanks!

Activity

  1. timsuchanek commented on Feb 23, 2021

    @timsuchanek
    Contributor

    Thanks for reporting @GCastilho!
    Could you share the specific query with us plus a subset of the schema that got slower?

  2. added
    bug/0-unknown
    kind/bug
    on Feb 23, 2021
  3. GCastilho commented on Feb 23, 2021

    @GCastilho
    Author

    For sure, @timsuchanek

    This is the schema:

    model User {
      @@map("users")
      id              String   @id @default(uuid())
      email           String   @unique
      password        String
      role            UserRole @default(USER)
      created_at      DateTime @default(now())
      updated_at      DateTime @updatedAt
      lotes           Lote[]
      criadores       Criador[]
      granjas         Granja[]
    }
    
    model Criador {
      @@map("criadores")
      id          Int
      userId      String
      user        User      @relation(fields: [userId])
      nome        String
      telefone    String
      created_at  DateTime  @default(now())
      updated_at  DateTime  @updatedAt
      granjas     Granja[]
      lotes       Lote[]
      @@id([userId, id])
    }

    And the queries logged by prisma:

    From the first test, [email protected]:

    prisma:info  Starting a postgresql pool with 5 connections.
    prisma:info  Fetched a connection from the pool
    prisma:info  Started http server on http://127.0.0.1:34325
    prisma:info  Fetched a connection from the pool
    prisma:query DELETE FROM users
    prisma:info  Fetched a connection from the pool
    prisma:query BEGIN
    prisma:query INSERT INTO "public"."users" ("id","email","password","role","created_at","updated_at") VALUES ($1,$2,$3,$4,$5,$6) RETURNING "public"."users"."id"
    prisma:query SELECT "public"."users"."id", "public"."users"."email", "public"."users"."password", "public"."users"."role", "public"."users"."created_at", "public"."users"."updated_at" FROM "public"."users" WHERE "public"."users"."id" = $1 LIMIT $2 OFFSET $3
    prisma:query COMMIT
    prisma:info  Fetched a connection from the pool
    prisma:query SELECT "public"."users"."id", "public"."users"."password", "public"."users"."role" FROM "public"."users" WHERE "public"."users"."email" = $1 LIMIT $2 OFFSET $3
    prisma:info  Fetched a connection from the pool
    prisma:query DELETE FROM criadores
    prisma:info  Fetched a connection from the pool
    prisma:query BEGIN
    prisma:query SELECT "public"."users"."id" FROM "public"."users" WHERE "public"."users"."id" = $1 OFFSET $2
    prisma:query INSERT INTO "public"."criadores" ("id","userId","nome","telefone","created_at","updated_at") VALUES ($1,$2,$3,$4,$5,$6) RETURNING "public"."criadores"."userId", "public"."criadores"."id"
    prisma:query SELECT "public"."criadores"."userId", "public"."criadores"."id", "public"."criadores"."nome", "public"."criadores"."telefone", "public"."criadores"."created_at", "public"."criadores"."updated_at" FROM "public"."criadores" WHERE ("public"."criadores"."userId" = $1 AND "public"."criadores"."id" = $2) LIMIT $3 OFFSET $4
    prisma:query COMMIT
    prisma:info  Fetched a connection from the pool
    prisma:query SELECT "public"."criadores"."userId", "public"."criadores"."id", "public"."criadores"."nome", "public"."criadores"."telefone", "public"."criadores"."created_at", "public"."criadores"."updated_at" FROM "public"."criadores" WHERE "public"."criadores"."id" = $1 LIMIT $2 OFFSET $3

    From the second test:

    prisma:info  Fetched a connection from the pool
    prisma:query DELETE FROM criadores
    prisma:info  Fetched a connection from the pool
    prisma:query BEGIN
    prisma:query SELECT "public"."users"."id" FROM "public"."users" WHERE "public"."users"."id" = $1 OFFSET $2
    prisma:query INSERT INTO "public"."criadores" ("id","userId","nome","telefone","created_at","updated_at") VALUES ($1,$2,$3,$4,$5,$6) RETURNING "public"."criadores"."userId", "public"."criadores"."id"
    prisma:query SELECT "public"."criadores"."userId", "public"."criadores"."id", "public"."criadores"."nome", "public"."criadores"."telefone", "public"."criadores"."created_at", "public"."criadores"."updated_at" FROM "public"."criadores" WHERE ("public"."criadores"."userId" = $1 AND "public"."criadores"."id" = $2) LIMIT $3 OFFSET $4
    prisma:query COMMIT
    prisma:info  Fetched a connection from the pool
    prisma:query BEGIN
    prisma:query SELECT "public"."criadores"."userId", "public"."criadores"."id" FROM "public"."criadores" WHERE ("public"."criadores"."userId" = $1 AND "public"."criadores"."id" = $2)
    prisma:query UPDATE "public"."criadores" SET "nome" = $1, "updated_at" = $2 WHERE ("public"."criadores"."userId","public"."criadores"."id") IN (($3,$4))
    prisma:query SELECT "public"."criadores"."userId", "public"."criadores"."id", "public"."criadores"."nome", "public"."criadores"."telefone", "public"."criadores"."created_at", "public"."criadores"."updated_at" FROM "public"."criadores" WHERE ("public"."criadores"."userId" = $1 AND "public"."criadores"."id" = $2) LIMIT $3 OFFSET $4
    prisma:query COMMIT
    prisma:info  Fetched a connection from the pool
    prisma:query SELECT "public"."criadores"."userId", "public"."criadores"."id", "public"."criadores"."nome", "public"."criadores"."telefone", "public"."criadores"."created_at", "public"."criadores"."updated_at" FROM "public"."criadores" WHERE "public"."criadores"."id" = $1 LIMIT $2 OFFSET $3

    The tests were performed using mocha and the generated "prisma:query" are identical between 2.14, 2.15 and 2.17, the only difference (just noticed it) are the "prisma:info" statements, where on 2.14 we only had these two:

    prisma:info  Starting a postgresql pool with 5 connections.
    prisma:info  Started http server on http+unix:///tmp/prisma-b055a1ccfdb2a3a62e187d82.sock
    
  4. timsuchanek commented on Feb 23, 2021

    @timsuchanek
    Contributor

    Thanks @GCastilho!
    One more thing: Could you show the Prisma Client query that you are sending?
    So in the form like this:

    prisma.user.findMany({ where: {} })

    Thanks!

  5. GCastilho commented on Feb 23, 2021

    @GCastilho
    Author

    For sure, @timsuchanek

    Here are the queries, in order:

    await prisma.user.create({
    	data: {
    		email,
    		password,
    		role,
    	}
    })
    
    await prisma.user.findUnique({
    	select: { id: true, password: true, role: true },
    	where: { email },
    })
    
    await prisma.criador.create({
    	data: {
    		...novoCriador,
    		userId,
    	}
    })
    
    await prisma.criador.findFirst({
    	where: { id: 1 }
    })
    
    await prisma.criador.update({
    	where: {
    		userId_id: {
    			userId: req.user.sub,
    			id: idCriador
    		}
    	},
    	data: updateCriador
    })

    Duplicated queries I've displayed only once. The delete queries are all used as

    await prisma.$queryRaw('DELETE FROM table')

    Because prisma (AFAIK) still does not have support for "deleteMany" with cascade relations

  6. Akxe commented on Mar 16, 2021

    @Akxe
    Contributor

    This does happen to me too! I have shared all info with @matthewmueller via email. My query however did go from less than a second to 30-40sec!

    Also, I would like to note, I did test every single query sent to DB and every one finished under 100ms (6 total)


    ctx.prisma.offer.findUnique({
      select: {
        offerID: true,
        title: true,
        periodsOfOffer: {
          select: { period: { select: { periodID: true, text: true } } }
        },
        carriersOfOffer: {
          select: {
            carrierID: true,
            shortID: true,
            type: true,
            sides: true,
            periods: {
              select: {
                selected: true,
                periodID: true,
                wanted: true,
                reservation: true,
                motiveID: true,
                sellingPrice: true,
                count: true,
                groups: { select: { groupID: true, motiveID: true } }
              }
            },
            carrier: {
              select: {
                lat: true,
                lng: true,
                city: true,
                street: true,
                traffic: true,
                description: true,
                districtID: true,
                regionID: true,
                orientation: true,
                photos: { select: { src: true, order: true } }
              }
            }
          }
        }
      },
      where: {
        offerID: args.offerID
      },
    })
  7. Tsvetomir-M commented on Mar 24, 2021

    @Tsvetomir-M

    A simple query on 2.19 is much slower than on 2.14.
    I have tested with this example.

    Environment:

    OS: Debian GNU/Linux 9 (stretch) 64-bit
    Database: PostgreSQL 13.0 (Debian 13.0-1.pgdg90+1)
    Node.js: v14.2.0
    

    Db model:

    model test {
      id   Int    @id @default(autoincrement()) @db.Integer
      name String @db.Text
    }
    

    Code:

    let start = new Date().getTime();
    
    for (let i = 0; i < 1000; i++) {
        let res = await prisma.test.findUnique({
            select: {
                id: true,
                name: true,
            },
            where: {
                id: 1
            }
        });
    }
    
    console.log(new Date().getTime() - start);
    

    Results:

    Prisma 2.14.0 - 4 seconds
    Prisma 2.19.0 - 56 seconds
    

    $queryRaw is slower as well.

  8. chinanderm commented on Mar 24, 2021

    @chinanderm

    I've also noticed considerably slower performance with queries.

  9. pantharshit00 commented on May 16, 2021

    @pantharshit00
    Contributor

    Hello everyone,

    Can you all still reproduce these regressions in the latest version of Prisma?

  10. Akxe commented on May 16, 2021

    @Akxe
    Contributor

    @pantharshit00 Yes, indeed.

    I am doing and measuring 5 simultaneous queries (+ one initial query)

    [GQL/clientOffer] 📑 "offerInfo": 135.357ms
    [GQL/clientOffer] 📑 "carriersPromise": 38.925s
    [GQL/clientOffer] 📑 "groups": 4.69ms
    [GQL/clientOffer] 📑 "districts": 5.264ms
    [GQL/clientOffer] 📑 "motives": 6.566ms
    [GQL/clientOffer] 📑 "regions": 6.359ms
    [GQL/clientOffer] 📑 "all": 38.933s
    

    carriersPromise query:

    {
      select: {
        carrierID: true,
        shortID: true,
        type: true,
        sides: true,
        periods: {
          select: {
            selected: true,
            periodID: true,
            wanted: true,
            reservation: true,
            motiveID: true,
            sellingPrice: true,
            count: true,
            groups: { select: { groupID: true, motiveID: true } }
          }
        },
        carrier: {
          select: {
            lat: true,
            lng: true,
            city: true,
            street: true,
            traffic: true,
            description: true,
            districtID: true,
            regionID: true,
            orientation: true,
            photos: { select: { src: true, order: true } }
          }
        },
        offerID: true
      },
      where: {
        offerID: 9042,
        carrier: { districtID: { in: undefined }, regionID: { in: undefined } }  }
    }

    The prod server is using 2.21.x. On the dev server, I tried the 2.22.x

    Dev Prisma version:

    Environment variables loaded from prisma\.env
    prisma               : 2.22.1
    @prisma/client       : 2.22.1
    Current platform     : windows
    Query Engine         : query-engine 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules\@prisma\engines\query-engine-windows.exe)
    Migration Engine     : migration-engine-cli 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules\@prisma\engines\migration-engine-windows.exe)   
    Introspection Engine : introspection-core 60cc71d884972ab4e897f0277c4b84383dddaf6c (at node_modules\@prisma\engines\introspection-engine-windows.exe) 
    Format Binary        : prisma-fmt 60cc71d884972ab4e897f0277c4b84383dddaf6c 
    (at node_modules\@prisma\engines\prisma-fmt-windows.exe)
    Default Engines Hash : 60cc71d884972ab4e897f0277c4b84383dddaf6c
    Studio               : 0.379.0
    
  11. 8 remaining items

  12. Akxe commented on Jun 23, 2021

    @Akxe
    Contributor

    @janpio Can confirm, no change in the duration of my (prisma) query.

  13. k-sae commented on Jun 23, 2021

    @k-sae

    @Akxe Have you run npx prisma generate after adding previewFeatures = ["nApi"] to schema.prisma?

  14. Akxe commented on Jun 23, 2021

    @Akxe
    Contributor

    @Akxe Have you run npx prisma generate after adding previewFeatures = ["nApi"] to schema.prisma?

    Indeed I did, yet no change. Still it resulted in 36-38 sec

  15. k-sae commented on Jun 23, 2021

    @k-sae

    @Akxe that is bad, the last thing I want is having performance issues on the server due to unknown parameters.
    I have conducted the test multiple times with logging disabled and the results are pretty close.
    Here some added information maybe it helps regards identifying the root cause of the issue.
    The database I am using is the employee database from here. It contains about 300,024 employee record
    Prisma schema:

    model employees {
      emp_no       Int              @id
      birth_date   DateTime         @db.Date
      first_name   String           @db.VarChar(14)
      last_name    String           @db.VarChar(16)
      gender       employees_gender
      hire_date    DateTime         @db.Date
    }
    

    The query generated from prisma:

    SELECT `employees`.`employees`.`emp_no`, `employees`.`employees`.`birth_date`, `employees`.`employees`.`first_name`, `employees`.`employees`.`last_name`, `employees`.`employees`.`gender`, `employees`.`employees`.`hire_date` FROM `employees`.`employees` WHERE 1=1 ORDER BY `employees`.`employees`.`emp_no` ASC LIMIT ? OFFSET ?

    The query generated from sequelize:

    SELECT `emp_no`, `birth_date`, `first_name`, `last_name`, `gender`, `hire_date` FROM `employees` AS `employees` LIMIT 100;

    I have run the both of the queries directly from the MySQL database and the result wasn't significantly large

    Results

    prisma raw query: 1.2ms
    sequelize raw query: 0.9ms
    

    The next step was to test the sequelize-query using prisma query.$raw:

     for (let index = 0; index < 2000; index++) {
      const allEmployeesRaw = await prisma.$queryRaw('SELECT `emp_no`, `birth_date`, `first_name`, `last_name`, `gender`, `hire_date` FROM `employees` AS `employees` LIMIT 100;');
     }

    Results

    prisma raw without nodeApi: 26.817s
    prisma raw with nodeApi: 4.452s
    

    Notice that in the case of raw prisma queries the performance improvement wasn't significant especially that I am now ignoring most of the prisma features.

  16. Akxe commented on Jun 23, 2021

    @Akxe
    Contributor

    @k-sae Just to clarify, I am not from Prisma. In my case the SQL Prisma creates is not the bottleneck.

    I ran my test. I did enable full SQL logging, and requested

    prisma.carrierOfOffer.findMany({
      select: {
        carrierID: true,
        shortID: true,
        type: true,
        periods: {
          select: {
            carrierID: true,
            groups: { select: { groupID: true, motiveID: true } }
          }
        },
        carrier: {
          select: {
            carrierID: true,
            regionID: true,
            districtID: true,
            lat: true,
            lng: true
          }
        },
        offerID: true,
      }
    });

    The SQL it produces is extremely long (and was posted previously), but when I copied the SQL and put it into MySQL workbench, it returned results under 0.1s. For me, the problem is within the processing of incoming result(s).

  17. janpio commented on Jun 24, 2021

    @janpio
    Contributor

    @k-sae As said above, your original problem was not the same as what this thread is about. You were experiencing a different problem, which is somehow solved by switching to Node-API - which will not help for the problem @Akxe and Co describe here. No need to investigate further here. (But you please still comment on that issue or open a new one, so we can understand better)

  18. k-sae commented on Jun 24, 2021

    @k-sae

    @janpio Thank you, I will create a reproduction here #6301, maybe it will help to identify this issue. I really appreciate the hard work of the Prisma team.

  19. peterrogov commented on Jan 12, 2022

    @peterrogov

    I am experiencing close to 10x difference in performance between operations executed by prisma and similar raw queries. I will make a reproduction repo soon. Just adding my voice for now. Prisma 3.8.0 and postgres 12 (macos)

  20. millsp commented on Aug 2, 2023

    @millsp
    Contributor

    Hey everyone, in Prisma 5 we have made considerable performance improvements. @GCastilho @peterrogov @k-sae @Akxe @chinanderm @Tsvetomir-M, if you are using Prisma 5 are you still experiencing these problems? Otherwise, I'd love if you could give it a try.

  21. millsp commented on Aug 9, 2023

    @millsp
    Contributor

    Hey everyone, given the age of this issue, the recent improvements in query and startup performance, and also the difficulty to reproduce, I am closing it. But don't take me wrong, if you are experiencing any performance regression, please open a new issue with the necessary information for us to reproduce it.

    Thanks.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug/0-unknownkind/bugtopic: performancetopic: prisma-client

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions