将数据导入Prisma / PostgreSQL服务的最有效方法?

问题描述 投票:0回答:1

我有一个Prisma(1.14.2)服务运行,附加到PostgreSQL数据库。我需要通过Prisma连接器插入许多与PostgreSQL数据库具有一对多关系的节点。现在我以下面的方式做这件事。 strokessamples数组拥有大量节点:

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 }
        }
    });
}

在每个笔划128个笔划和128个样本的情况下(即总共16348个样本),这需要大约38秒。

我想知道是否有办法加快这个过程?特别是因为笔画和样本的数量可以变得更高。我可以使用prisma import ...,它显示了6倍的加速。但我想避免所需的转换为Normalized Data Format (NDF)

我读到了加速PostgreSQL in general中的INSERT,但我不确定是否以及如何将其应用于Prisma连接器。

postgresql prisma
1个回答
0
投票

往返(查询您的API,查询数据库并返回值)需要花费大量时间。实际上,它确实需要比实际SQL查询更多的时间。

要解决您的问题,您应该批量查询。有很多方法可以实现,您可以使用GraphQL查询批处理包,或者只需这样做:

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

请记住,Prisma will time out queries taking longer than 45s,因此您可能希望限制批次大小。

给定每批次的查询数量,它将将往返次数除以n。

© www.soinside.com 2019 - 2024. All rights reserved.