1

I have a Prisma (1.14.2) service running that is attached to a PostgreSQL database. I need to insert a lot of nodes with a one-to-many relation to the PostgreSQL database via the Prisma connector. Right now I am doing this in the following way. The strokes and samples arrays hold a lot of nodes:

for (let strokeIndex = 0; strokeIndex < painting.strokes.length; strokeIndex++) {
    const stroke = painting.strokes[strokeIndex];
    const samples = stroke.samples;
    const createdStroke = await PrismaServer.mutation.createStroke({
        data: {
            myId: stroke.id,
            myCreatedAt: new Date(stroke.createdAt),
            brushType: stroke.brushType,
            color: stroke.color,
            randomSeed: stroke.randomSeed,
            painting: {connect: {myId: jsonPainting.id}},
            samples: { create: samples }
        }
    });
}

In the case of 128 strokes and 128 samples for each stroke (i.e. 16348 samples in total) this takes about 38 seconds.

I am wondering if there is a way to speed up the process? Especially since the number of strokes and samples can get much higher. I could use prisma import ..., which showed a 6x speedup. But I want to avoid the required conversion to the Normalized Data Format (NDF).

I read about speeding up the INSERTs in PostgreSQL in general, but I am not sure if and how I can apply that to the Prisma connector.

1awuesterose
  • 495
  • 5
  • 15
  • 1
    I couldn't post this before you deleted your latest question so just letting you know here: FWIW, if you have a `std::string` or `std::string_view` you can just use `string_variable.front() == string_variable.back()` not pay all the cost needed for a regex – NathanOliver Feb 27 '19 at 22:20

2 Answers2

0

Round trips (querying your API, that query your database, and returns the values) takes a lot of time. Actually, it certainly takes more time than the actual SQL query.

To solve your problem, you should probably batch your queries. There are many ways to do it, you can use a GraphQL query batching package, or simply do it like this :

mutation {
  q1: createStroke(color: "red") {
    id
  }
  q2: createStroke(color: "blue") {
    id
  }
}

Remember that Prisma will time out queries taking longer than 45s, therefore you may want to limit your batches size.

Given n the number of queries per batch, it will divide the number of round trips by n.

Namoz
  • 540
  • 2
  • 14
0

Since Prisma version 2.20.0, you should be able to use .createMany({}) now. Probably this answer won't be helpful since you asked 3years ago...

https://www.prisma.io/docs/concepts/components/prisma-client/crud#create-multiple-records

victorkurauchi
  • 1,390
  • 18
  • 23