diff --git a/lib/microwaveprop/weather.ex b/lib/microwaveprop/weather.ex index 5ab7d804..f87fb2ea 100644 --- a/lib/microwaveprop/weather.ex +++ b/lib/microwaveprop/weather.ex @@ -211,19 +211,26 @@ defmodule Microwaveprop.Weather do def upsert_hrrr_profiles_batch(profiles) do now = DateTime.truncate(DateTime.utc_now(), :second) - entries = - Enum.map(profiles, fn attrs -> - Map.merge(attrs, %{ - id: Ecto.UUID.generate(), - inserted_at: now, - updated_at: now - }) - end) + profiles + |> Enum.chunk_every(500) + |> Enum.reduce({0, nil}, fn chunk, {total_count, _} -> + entries = + Enum.map(chunk, fn attrs -> + Map.merge(attrs, %{ + id: Ecto.UUID.generate(), + inserted_at: now, + updated_at: now + }) + end) - Repo.insert_all(HrrrProfile, entries, - on_conflict: {:replace_all_except, [:id, :inserted_at]}, - conflict_target: [:lat, :lon, :valid_time] - ) + {count, rows} = + Repo.insert_all(HrrrProfile, entries, + on_conflict: {:replace_all_except, [:id, :inserted_at]}, + conflict_target: [:lat, :lon, :valid_time] + ) + + {total_count + count, rows} + end) end def has_hrrr_profile?(lat, lon, valid_time) do diff --git a/priv/repo/migrations/20260403142241_analyze_all_tables.exs b/priv/repo/migrations/20260403142241_analyze_all_tables.exs new file mode 100644 index 00000000..e9766082 --- /dev/null +++ b/priv/repo/migrations/20260403142241_analyze_all_tables.exs @@ -0,0 +1,9 @@ +defmodule Microwaveprop.Repo.Migrations.AnalyzeAllTables do + use Ecto.Migration + + def up do + execute "ANALYZE" + end + + def down, do: :ok +end