-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Insert all on a collection with the same timestamp (#2712)
* Add all records with same timestamp * Fix parallel collections put all benchmarking * Replace Ecto update with CTE based SQL query * Fix formatting --------- Co-authored-by: Stuart Corbishley <[email protected]>
- Loading branch information
Showing
11 changed files
with
128 additions
and
39 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -30,6 +30,8 @@ and this project adheres to | |
|
||
### Changed | ||
|
||
- Insert all on a collection with the same timestamp | ||
[#2711](https://github.com/OpenFn/lightning/issues/2711) | ||
- AI Assistant: Show disclaimer once every day per user | ||
[#2481](https://github.com/OpenFn/lightning/issues/2481) | ||
- AI Assistant: Scroll to new message when it arrives | ||
|
@@ -56,9 +58,8 @@ and this project adheres to | |
- Allow filtering collection items by updated_before and updated_after. | ||
[#2693](https://github.com/OpenFn/lightning/issues/2693) | ||
- Add support for SMTP email configuration | ||
[#2699](https://github.com/OpenFn/lightning/issues/2699) | ||
⚠️️ Please note that `EMAIL_ADMIN` defaults to `[email protected]` in | ||
production environments | ||
[#2699](https://github.com/OpenFn/lightning/issues/2699) ⚠️️ Please note that | ||
`EMAIL_ADMIN` defaults to `[email protected]` in production environments | ||
|
||
### Fixed | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,12 +7,15 @@ IO.puts "### Indexes on collection_items table" | |
Enum.each(rows, &IO.puts(Enum.join(&1, ":\n"))) | ||
|
||
keys_count = 5_000 | ||
rounds = 300 | ||
# keys_count = 10_000 | ||
# rounds = 1 | ||
|
||
Repo.delete_all(Collections.Collection) | ||
|
||
project = | ||
with nil <- Repo.get_by(Projects.Project, name: "bench") do | ||
user = Repo.get_by(Lightning.Accounts.User, email: "[email protected]") | ||
user = Repo.get_by(Lightning.Accounts.User, email: "[email protected]") || raise "This benchmark requires demo/known user" | ||
{:ok, project} = Projects.create_project(%{name: "bench", project_users: [%{user_id: user.id, role: :owner}]}) | ||
project | ||
end | ||
|
@@ -49,7 +52,6 @@ record2 = fn prefix, i -> | |
} | ||
end | ||
|
||
rounds = 300 | ||
samples1 = | ||
1..keys_count * rounds | ||
|> Enum.map(fn i -> record1.("keyA", i) end) | ||
|
@@ -60,21 +62,22 @@ samples2 = | |
|> Enum.map(fn i -> record2.("keyB", i) end) | ||
|> Enum.chunk_every(keys_count) | ||
|
||
IO.puts("\n### Inserting #{rounds} rounds of 2x5000 with put_all...") | ||
IO.puts("\n### Inserting #{rounds} rounds of 2x#{keys_count} with put_all...") | ||
|
||
durations = | ||
Enum.zip(samples1, samples2) | ||
|> Enum.with_index(fn {sample1, sample2}, i -> | ||
:timer.tc(fn -> | ||
{:ok, _n} = Collections.put_all(collection1, sample1) | ||
{:ok, _n} = Collections.put_all(collection2, sample2) | ||
end) | ||
|> then(fn {duration, _res} -> | ||
duration_ms = div(duration, 1_000) | ||
IO.puts("[#{i}] elapsed time: #{duration_ms}ms") | ||
|> Enum.flat_map(fn {sample1, sample2} -> [{sample1, collection1}, {sample2, collection2}] end) | ||
|> Task.async_stream(fn {sample, collection} -> | ||
{duration, _res} = :timer.tc(fn -> | ||
{:ok, _n} = Collections.put_all(collection, sample) | ||
end) | ||
|
||
div(duration, 1_000) | ||
end, max_concurrency: 2, timeout: :infinity) | ||
|> Enum.map(fn {:ok, duration_ms} -> | ||
IO.puts("elapsed time: #{duration_ms}ms") | ||
duration_ms | ||
end) | ||
end) | ||
|
||
IO.puts "Average: #{Statistics.mean(durations)}ms" | ||
IO.puts "Std Deviation: #{Statistics.stdev(durations)}ms" |
11 changes: 11 additions & 0 deletions
11
priv/repo/migrations/20241121160001_create_collection_items_id.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Lightning.Repo.Migrations.CreateCollectionItemsSerialId do | ||
use Ecto.Migration | ||
|
||
def change do | ||
alter table(:collection_items) do | ||
add :id, :bigint | ||
end | ||
|
||
create unique_index(:collection_items, [:collection_id, :id]) | ||
end | ||
end |
22 changes: 22 additions & 0 deletions
22
priv/repo/migrations/20241121160002_set_collection_items_id.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
defmodule Lightning.Repo.Migrations.SetCollectionItemsSerialId do | ||
use Ecto.Migration | ||
|
||
def up do | ||
execute """ | ||
WITH ordered_rows AS ( | ||
SELECT collection_id, key, row_number() OVER () AS rn | ||
FROM collection_items | ||
ORDER BY inserted_at ASC | ||
) | ||
UPDATE collection_items | ||
SET id = ordered_rows.rn | ||
FROM ordered_rows | ||
WHERE collection_items.collection_id = ordered_rows.collection_id | ||
AND collection_items.key = ordered_rows.key; | ||
""" | ||
end | ||
|
||
def down do | ||
:ok | ||
end | ||
end |
25 changes: 25 additions & 0 deletions
25
priv/repo/migrations/20241121160003_create_collection_items_sequence.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
defmodule Lightning.Repo.Migrations.CollectionItemsSequence do | ||
use Ecto.Migration | ||
@disable_migration_lock true | ||
@disable_ddl_transaction true | ||
|
||
def up do | ||
execute("CREATE SEQUENCE collection_items_id_seq") | ||
|
||
alter table(:collection_items) do | ||
modify :id, :bigint, | ||
null: false, | ||
default: fragment("nextval('collection_items_id_seq'::regclass)") | ||
end | ||
|
||
execute("SELECT setval('collection_items_id_seq'::regclass, MAX(id)) FROM collection_items") | ||
end | ||
|
||
def down do | ||
alter table(:collection_items) do | ||
modify :id, :bigint, null: true, default: nil | ||
end | ||
|
||
execute("DROP SEQUENCE collection_items_id_seq") | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters