sezaru

sezaru

So, I’m importing millions of rows in my system to a resource that has some fields (around 7 fields) that are embedded resources.

I’m inserting the fields using bulk_create with chunks of 10_000 rows.

I noticed that this operation was kinda slow, it takes around 36 seconds to insert the 10_000 rows.

I suspected that the issue could be because of the embedded resources, so I removed them and replaced with fields directly in the main resource.

After that, the same bulk_create call takes 6 seconds to do the same work.

So, it seems that a embedded resource creates a big overhead when inserting a bunch of data.

Zack, do you think there is something that can be done to improve that, or that overhead is to be expected?

Showing Posts 1 to 10

zachdaniel

zachdaniel

Creator of Ash

This definitely strikes me as something that should be fixable. It’s possible that the overhead is in the data layer itself, but I think that is probably unlikely. I would need to investigate further, and may not have time to do that this week. You can confirm that the performance issue is Ash by orchestrating a similar operation with bare ecto if you have some time :slight_smile:

sezaru

sezaru OP

I will try that when I have some free time and report back to you :smiley:

zachdaniel

zachdaniel

Creator of Ash

So, someone else reported a similar issue with casting embedded resources. I hope to have some time to look into it later this week.

barnabasJ

barnabasJ

Ash Core Team

I had a similar issue. I had a calculation in which I made an API request and used a resource with Ash.DataLayer.Simple to convert the data in the response into a list of Ash Resources to have nice Graphql types.

The Resource in question also had a couple of embedded resources and also nested embedded resources. This was also really slow.

The only way I was able to get it to a reasonable timeframe was to do all the conversions myself and create the structs for the embedded resources myself before passing the data to API.bulk_create with the assume_casted?: true option.

zachdaniel

zachdaniel

Creator of Ash

Alright, beginning to look into this issue. There is definitely a significant slow down on embedded resource casting

Name             ips        average  deviation         median         99th %
maps         23.69 K      0.0422 ms    ±56.44%      0.0393 ms       0.107 ms
embeds        0.25 K        4.06 ms     ±4.56%        4.03 ms        4.66 ms
zachdaniel

zachdaniel

Creator of Ash

Okay so I’ve made some performance improvements. They are still much slower than raw maps, but if you guys could try it out and report back, that would be great. Performance improvements are in main. If you update ash to main, make sure to update ash_postgres and ash_graphql also.

barnabasJ

barnabasJ

Ash Core Team

After getting everything working with ash/main again, I can say that our calculation is now about 3x faster than before.

zachdaniel

zachdaniel

Creator of Ash

Thats progress! Are you still having to manually create the embedded resources?

barnabasJ

barnabasJ

Ash Core Team

I do for now, as I’m still using assume_casted?: true for the bulk_create. Casting ist still taking up too much time in this specific instance.

I just finished upgrading, so I haven’t done any extensive testing yet but here is what I have right now.

I have these 2 reads, the first one is what I tried first but wasn’t fast enough.

I did a little testing with benchee, running the actions with the same list of 7 options each, so I don’t think bulk_create should a lot of optimization there.

Reading with doing the casting:

    read :read_options do
      argument :options, {:array, :map} do
        allow_nil? false
      end

      prepare before_action(fn query ->
                Simple.set_data(
                  query,
                  Ash.Query.get_argument(query, :options)
                  |> Enum.map(&__MODULE__.from_option!/1)
                )
              end)
    end

Takes around 10ms

vs

Preparing the input myself + doing this read action

      prepare before_action(fn query ->
                Simple.set_data(
                  query,
                  Ash.Query.get_argument(query, :availabilities)
                  |> Api.bulk_create(
                    __MODULE__,
                    :create,
                    assume_casted?: true,
                    return_records?: true,
                    return_errors?: true,
                    authorize?: false,
                    max_concurrency: 10
                  )
                  |> then(fn %Ash.BulkResult{records: records, errors: errors} ->
                    records
                  end)
                )
              end)
    end

Takes around 1ms

The thing with assume_casted?: true is that the embedded resources already need to be the real Ash.Resource structs. It might be nice to have an option where it would turn the maps into the structs but wouldn’t cast the values for it.

zachdaniel

zachdaniel

Creator of Ash

Interesting…I definitely want to track down the main slow down. I think there is likely just some unnecessary work being done there.

Where Next? Top

Trending in Questions Top

Blokh
Hey guys, I’ve got a huge CSV ( around 10 GB ) that needs to be processed hourly Do you guys have any suggestions what is the best prac...
New
kszambelanczyk
Hello! Could someone please give me a help/sample code, how to delete a file from s3 using waffle/waffle_ecto from Phoenix app. I creat...
New
RemyXRenard
I’m seeing that a list inside a Kino.DataTable will be interpreted as a charlist, even if the Kino.configure() is set to charlists: :as_l...
New
matt-savvy
Anyone here using Honeybadger? My Honeybadger account is being overwhelmed with noise from some bots. Seeing a lot of Bandit.HTTPError...
New
samoloth
Hi, I’ve just set up an application with ash_authentication. There is only magic link strategy for now, so there is no confirmation add o...
New
velrest
So my question is quite simple and i have found no conclusive answer on forum, google or AI. Should we use :erlang.float for Integer to ...
New
FlyingNoodle
If a change or preparation module uses Ash.Changeset.get_argument/2 or Ash.Query.get_argument/2 (or any of the other get_argument functio...
New

Other Trending Topics Top

mudasobwa
I am happy to introduce the very α version of the new programming language compiled to BEAM. Welcome Cure. It has literally three kille...
New
garrison
Hobbes is a low-level distributed database for the Elixir programming language. Hobbes provides a simple, safe, and scalable storage lay...
New
mcass19
ExRatatui lets you cook up rich terminal UIs in Elixir, powered by Rust’s ratatui via Rustler NIFs. Build interactive terminal applicatio...
New
Damirados
Hello everyone. After busy few months I am happy to announce v0.1.0 of Emerge & Solve. They are GUI (Emerge) and State management (S...
New
netoum
Corex is an accessible, unstyled UI component library for Phoenix that integrates Zag.js state machines using Vanilla JavaScript and Live...
New
wintermeyer
There are three potential reasons for members of this forum to have a look at https://vutuv.de You are tired or annoyed of LinkedIn. Yo...
New

We're in Beta

About us Mission Statement

Options

Thread Display Mode




Thread Preview

Skip Thread Previews