MEHUL.JAIN

MEHUL.JAIN

Insert into Select from Unnest

I have a use case where I want to insert large number of rows in PG Table at once from Ecto. But ecto has a param limit of 65_535, so the max rows I can insert is 65_535 / tot_number_of_fields_in_schema.

To overcome this, I am trying to see if I can to
Repo.insert_all(Schema, query)
query = from(c in Ecto.Query.fragment("SELECT * FROM unnest(? as field1, ? as field2"), ^field1_array, ^field2_array)

The idea here is, the total params will be 2 in this case

-- before
INSERT INTO users
    (id, name)
    VALUES ($1,$2),($3,$4);

-- after
INSERT INTO users
    (id, name)
    (select * from unnest($1::int[], $2::int[]));

Most Liked

LostKobrakai

LostKobrakai

Take a look at:

Though I also found jsonb_to_recordset quite useful:

Last Post!

MEHUL.JAIN

MEHUL.JAIN

I have done this:

"d" 
|> with_cte("d", as: fragment("SELECT * FROM unnest(ARRAY[1]::int[], ARRAY[2]::int[]) as x(a, b)"))
|> select([d], %{a: d.a})

But how do I remove prefix from cte query? I have a multi tenant/schema application, so I need prefix when I would try to do
DBWrapper.insert_all(SchemaModule, query)

Where Next?

Popular in Questions Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
PeterCarter
There are pre-rolled solutions for other frameworks that do work. However, Phoenix does not seem to have these. Have people had good expe...
New
Lily
In templates/appointment/index.html.eex: <%= for appointment <- @appointments do %> <tr> <td><%= appoi...
New
Fl4m3Ph03n1x
About me? ( if you have nothing better to do than reading about some random guy in the internet :stuck_out_tongue: ) Hello all, this is ...
New
gshaw
What is the idiomatic way of matching for not nil in Elixir? E.g., First way: defp halt_if_not_signed_in(conn, signed_in_account) when...
New
belgoros
I’m not a pro in using Regex and can’t figure out why the following behaviour happens, especially if we take into account the difference ...
New
svb
Hi! Currently I want to submit a form by pressing the Enter key. However, since my input field is of type “textarea” this is just adds a...
New

Other popular topics Top

electic
Hi, I am new to Elixir. I am trying to use the DateTime component to insert a date into MySQL however the there seems to be no way to fo...
New
vonH
In asking this question I am more interested about the expressiveness of the language itself and less concerned about the availability of...
New
nsuchy
Hi. I’ve noticed that Windows Powershell has it’s own IEX command and you cannot access Elixir’s IEX due to the conflict. This isn’t a cr...
New
chrismccord
Phoenix 1.4.0 released Phoenix 1.4 is out! This release ships with exciting new features, most notably with HTTP2 support, improved deve...
688 31525 112
New
shijith.k
I am trying to start a new phoenix project with elixir 1.9, but mix phx.new does not work. It says that ** (Mix) The task "phx.new" could...
New
jason.o
In the code below, if the create action is not set to accept “extra_key” as an input, it errors out with a message shown above. Is there ...
New

We're in Beta

About us Mission Statement