【发布时间】:2021-09-19 03:48:14
【问题描述】:
我正在尝试使用Ecto.Multi.new 在数据库中插入1_000_000 记录。
alias Remote.Repo
datetime = DateTime.utc_now()
Enum.map(1..1_000_000, fn _x -> %{points: 0, inserted_at: datetime, updated_at: datetime} end)
|> Enum.chunk_every(65535)
|> Enum.each(fn rows ->
Ecto.Multi.new()
|> Ecto.Multi.insert_all(:insert_all, Users, rows)
|> Remote.Repo.transaction()
end)
以上是总代码。当我不使用Enum.chunk_every/2 时,它向我抛出了错误
** (Postgrex.QueryError) postgresql protocol can not handle 1000000 parameters, the maximum is 65535
但后来我开始使用|> Enum.chunk_every(65535) 现在它会抛出错误
** (Postgrex.QueryError) postgresql protocol can not handle 196605 parameters, the maximum is 65535
现在我正在使用|> Enum.chunk_every(6000),它工作正常。
问题:
- 这是
Enum.chunk_every/2的正确行为吗? - 在 DB 中插入数据是正确且最快的方法吗?
- 您能否建议一种比这更快的方法来将行插入 DB?
【问题讨论】: