-
Notifications
You must be signed in to change notification settings - Fork 107
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: denormalize addresses on transactions to improve read (#351)
* wip: test to denormalize addresses on transactions to improve read performance * fix: sql insert * fix: extension init * fix: sqlite support * clean: some debug * fix: remove insertTransaction segfault protection Co-authored-by: Geoffrey Ragot <[email protected]> Co-authored-by: Antoine Gelloz <[email protected]>
- Loading branch information
1 parent
f103f3b
commit a55756b
Showing
7 changed files
with
171 additions
and
19 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
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
63 changes: 63 additions & 0 deletions
63
pkg/storage/sqlstorage/migrates/16-denormalize-addresses/any_test.go
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,63 @@ | ||
package _16_denormalize_addresses | ||
|
||
import ( | ||
"context" | ||
"testing" | ||
"time" | ||
|
||
"github.com/huandu/go-sqlbuilder" | ||
"github.com/numary/ledger/pkg/ledgertesting" | ||
"github.com/numary/ledger/pkg/storage/sqlstorage" | ||
"github.com/pborman/uuid" | ||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestMigrate16(t *testing.T) { | ||
driver, closeFunc, err := ledgertesting.StorageDriver() | ||
require.NoError(t, err) | ||
defer closeFunc() | ||
|
||
require.NoError(t, driver.Initialize(context.Background())) | ||
store, _, err := driver.GetLedgerStore(context.Background(), uuid.New(), true) | ||
require.NoError(t, err) | ||
|
||
schema := store.Schema() | ||
|
||
migrations, err := sqlstorage.CollectMigrationFiles(sqlstorage.MigrationsFS) | ||
require.NoError(t, err) | ||
|
||
modified, err := sqlstorage.Migrate(context.Background(), schema, migrations[0:16]...) | ||
require.NoError(t, err) | ||
require.True(t, modified) | ||
|
||
now := time.Now().UTC().Truncate(time.Second) | ||
|
||
ib := sqlbuilder.NewInsertBuilder() | ||
sqlq, args := ib. | ||
InsertInto(schema.Table("transactions")). | ||
Cols("id", "timestamp", "postings", "metadata"). | ||
Values(0, now.Format(time.RFC3339), `[ | ||
{"source": "world", "destination": "bank", "asset": "USD", "amount": 100}, | ||
{"source": "bank", "destination": "user", "asset": "USD", "amount": 100} | ||
]`, "{}"). | ||
BuildWithFlavor(schema.Flavor()) | ||
_, err = schema.ExecContext(context.Background(), sqlq, args...) | ||
require.NoError(t, err) | ||
|
||
modified, err = sqlstorage.Migrate(context.Background(), schema, migrations[16]) | ||
require.NoError(t, err) | ||
require.True(t, modified) | ||
|
||
sqlq, args = sqlbuilder. | ||
Select("sources", "destinations"). | ||
From(schema.Table("transactions")). | ||
Where("id = 0"). | ||
BuildWithFlavor(schema.Flavor()) | ||
|
||
row := store.Schema().QueryRowContext(context.Background(), sqlq, args...) | ||
require.NoError(t, row.Err()) | ||
var sources, destinations string | ||
require.NoError(t, err, row.Scan(&sources, &destinations)) | ||
require.Equal(t, "world;bank", sources) | ||
require.Equal(t, "bank;user", destinations) | ||
} |
21 changes: 21 additions & 0 deletions
21
pkg/storage/sqlstorage/migrates/16-denormalize-addresses/postgres.sql
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,21 @@ | ||
--statement | ||
alter table "VAR_LEDGER_NAME".transactions add column sources text; | ||
--statement | ||
alter table "VAR_LEDGER_NAME".transactions add column destinations text; | ||
--statement | ||
create index transactions_sources ON "VAR_LEDGER_NAME".transactions USING GIN (sources gin_trgm_ops); | ||
--statement | ||
create index transactions_destinations ON "VAR_LEDGER_NAME".transactions USING GIN (destinations gin_trgm_ops); | ||
--statement | ||
update "VAR_LEDGER_NAME".transactions | ||
set sources = ( | ||
select string_agg(ele->>'source', ';') | ||
from "VAR_LEDGER_NAME".transactions sub | ||
cross join lateral jsonb_array_elements(postings) source(ele) | ||
where transactions.id = sub.id | ||
), destinations = ( | ||
select string_agg(ele->>'destination', ';') | ||
from "VAR_LEDGER_NAME".transactions sub | ||
cross join lateral jsonb_array_elements(postings) source(ele) | ||
where transactions.id = sub.id | ||
); |
15 changes: 15 additions & 0 deletions
15
pkg/storage/sqlstorage/migrates/16-denormalize-addresses/sqlite.sql
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,15 @@ | ||
--statement | ||
alter table transactions add column sources text; | ||
--statement | ||
alter table transactions add column destinations text; | ||
--statement | ||
UPDATE transactions | ||
SET sources = ( | ||
select group_concat(json_extract(json_each.value, '$.source'), ';') | ||
from transactions tx2, json_each(tx2.postings) | ||
where transactions.id = tx2.id | ||
), destinations = ( | ||
select group_concat(json_extract(json_each.value, '$.destination'), ';') | ||
from transactions tx2, json_each(tx2.postings) | ||
where transactions.id = tx2.id | ||
); |
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