代码之家  ›  专栏  ›  技术社区  ›  1awuesterose

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

  •  0
  • 1awuesterose  · 技术社区  · 7 年前

    我有一个连接到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倍的加速。但我想避免必要的转换到 Normalized Data Format (NDF) .

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

    1 回复  |  直到 7 年前
        1
  •  0
  •   Namoz    7 年前

    查询API需要花费很多时间。 实际上,它肯定比实际的SQL查询花费更多的时间。

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

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

    还记得普里斯玛吗 will time out queries taking longer than 45s ,因此您可能需要限制批次大小。

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

        2
  •  0
  •   victorkurauchi    4 年前

    从Prisma 2.20.0版开始,您应该能够使用 .createMany({}) 现在 也许这个答案不会有帮助,因为你在30年前问过。。。

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