0

我有一个附加到 PostgreSQL 数据库的 Prisma (1.14.2) 服务正在运行。我需要通过 Prisma 连接器插入许多与 PostgreSQL 数据库具有一对多关系的节点。现在我正在通过以下方式执行此操作。和strokes数组samples包含很多节点:

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 倍的加速。但我想避免所需的转换为标准化数据格式 (NDF)

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

4

2 回答 2

0

从 Prisma 版本 2.20.0 开始,您现在应该可以使用.createMany({})了。自从你 3 年前问过这个问题以来,这个答案可能没有帮助......

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

于 2021-07-29T05:43:07.667 回答
0

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

要解决您的问题,您可能应该批量查询。有很多方法可以做到这一点,您可以使用 GraphQL 查询批处理包,或者简单地这样做:

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

请记住,Prisma将使查询时间超过 45s,因此您可能希望限制批量大小。

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

于 2018-09-04T13:18:15.487 回答