I have some code that looks like this. What it does it takes series, of chunked data. So around 10-15 chunks of data. each chunk is 5000 items in length, and each item have 10 attributes.
This was my attempt to trying to save the data into Mnesia.
data
|> Enum.each(fn row ->
row_data = [
transaction_id
| [event_id | Enum.map(attributes, fn attr -> row[Atom.to_string(attr)] end)]
]
[source | row_data]
|> List.to_tuple()
|> :mnesia.dirty_write()
end)
What I am finding out now is that it takes extremely long time for this to complete the entire operation. like 8-12 minutes to finish. First I suspect maybe because Mnesia write is using transactions and all the other complicated bells and whistles.
So I changed the last bit to use mnesia.ets/2, which bypasses the transaction and all the callbacks and redirectly write to ets, its slightly faster… but its still finishes in minutes.
data
|> Stream.each(fn row ->
row_data = [
transaction_id
| [event_id | Enum.map(attributes, fn attr -> row[Atom.to_string(attr)] end)]
]
processed_data =
[source | row_data]
|> List.to_tuple()
:mnesia.ets(fn ->
:mnesia.dirty_write(processed_data)
end)
end)
|> Stream.run()
What Am i doing wrong here? I thought mnesia/ets is suppose to be fast? Could it because I am iterating through 5000 items one at a time and perform the write? and mnesia is backlogged on write?
Whats the best course of action here for me?