--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/5f1ca29c-f615-45be-b5b2-706adc5b9b4an%40googlegroups.com.
👋I did a quick test in https://github.com/ruslandoga/insert_all_bench and it seems like it's indeed an improvement when lots of rows are being inserted. It almost works with insert_all(table, ecto_query) but I don't know how to index into unnest tuples.
Single array parameters are faster than a long list of values
that all need to be parsed. The parser is slower and more memory
hungry when dealing with long lists compared to the array input
function.
We use in our code base mostly the unnest trick for larger inserts for tables with lots of columns instead of using Repo.insert_all for 2 reasons:
1) The query is faster for the reason listed above
2) We don't have to pre chunk the stream so we don't hit the 65K
limit
If Repo.insert_all would use the unnest trick, that would be a
nice improvement.
Best regards,
Dario
--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/CACW5TXQr5Uh8dxgQ5Xjq6SA2F%2BgeqBP9nrE-Y3A5JXpGPBYDCA%40mail.gmail.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/3354d438-4467-43d0-bc64-1f80b247dac4%40gmail.com.
Hi Jose!I meant that it already kind of works with he current insert_all implementation but the query is not as clean as in the blog post since we need to select the fields explicitly and unnest with multiple arrays doesn't seem to allow indexing into the tuples. My workaround (https://github.com/ruslandoga/insert_all_bench/blob/07bd1b4fce46944d09d81f00a68f6ede279d6193/lib/insert_all_bench.ex#L18-L38) is using a cte with multiple unnest (one for each array), the performance seems to be the same as with the simpler query from the blog post  q =
   "input"
   |> with_cte("input",
    as: fragment("select unnest(?::int[]) as id, unnest(?::int[]) as name", ^ids, ^names)
   )
   |> select([i], %{id: i.id, name: i.name})
  :timer.tc(fn -> Repo.insert_all("users", q) end)So I wonder if we should aim at making this approach easier and documenting it of if it should be a new insert_all function. It probably wouldn't be able to completely replace the current insert_all as we can't evaluate SQL in the arrays but can in VALUES so all of these would be unsupported https://github.com/elixir-ecto/ecto_sql/blob/629b6633bb46e4b3b966ba9fedcf8a2296ed7c10/lib/ecto/adapters/postgres/connection.ex#L303-L310
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/bc5e651c-f5e9-499b-a1e6-5854eed5330en%40googlegroups.com.
> Both array and (?, ?) are sent as parameters, so no additional parsing in either case?
The difference is the amount of parameters there. If you have a 10 column table and you're inserting 1.000 rows.
with the current way, postgres needs to parse 10.000 parameters. with the unnest/array aproach, postgres have to parse 10.I was trying to do something directly with ecto, but to do that in the Ecto.Adapters.SQL.Connection.insert/7 implementation for postgres, we need to have the the types of the columns in the headers list.I don't see a way to change that without introducing a breaking change to ecto protocols.Maybe having a helper or an easy way to do the query as Ruslan suggested would be a simple approach.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/d4602a81-5552-40bf-ae30-5cd7cbb80854n%40googlegroups.com.
--
You received this message because you are subscribed to the Google Groups "elixir-ecto" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-ecto...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-ecto/ea4e6bf0-615b-4304-b2d0-12ec2a767097n%40googlegroups.com.