From 5f2990de0287b59a1a9608471f55cc31092f346b Mon Sep 17 00:00:00 2001 From: Maxence Maireaux Date: Tue, 4 Jul 2023 11:57:43 +0200 Subject: [PATCH] fix: add new indexes & rework distinct (#449) fix: Improve Indexes & Transactions query --- .goreleaser-darwin.yml | 19 ++++++++----------- .goreleaser.yml | 16 +++++++--------- .../20-optimized-indexes/postgres.sql | 8 ++++++++ pkg/storage/sqlstorage/transactions.go | 8 ++++++-- 4 files changed, 29 insertions(+), 22 deletions(-) create mode 100644 pkg/storage/sqlstorage/migrates/20-optimized-indexes/postgres.sql diff --git a/.goreleaser-darwin.yml b/.goreleaser-darwin.yml index f733dc0e2..f72c4b051 100644 --- a/.goreleaser-darwin.yml +++ b/.goreleaser-darwin.yml @@ -33,16 +33,13 @@ archives: format_overrides: - goos: windows format: zip - name_template: "{{.ProjectName}}_{{.Version}}_{{.Os}}-{{.Arch}}" - replacements: - amd64: 64bit - 386: 32bit - arm: ARM - arm64: ARM64 - darwin: macOS - linux: Linux - windows: Windows - + name_template: >- + {{ .ProjectName }}_ + {{- title .Os }}_ + {{- if eq .Arch "amd64" }}x86_64 + {{- else if eq .Arch "386" }}i386 + {{- else }}{{ .Arch }}{{ end }} + {{- if .Arm }}v{{ .Arm }}{{ end }} checksum: name_template: 'checksums-darwin.txt' @@ -51,7 +48,7 @@ snapshot: name_template: "{{ .Tag }}" brews: - - tap: + - repository: owner: formancehq name: homebrew-tap name: numary diff --git a/.goreleaser.yml b/.goreleaser.yml index 51beb5193..07e9a80ec 100644 --- a/.goreleaser.yml +++ b/.goreleaser.yml @@ -67,15 +67,13 @@ archives: format_overrides: - goos: windows format: zip - name_template: "{{.ProjectName}}_{{.Version}}_{{.Os}}-{{.Arch}}" - replacements: - amd64: 64bit - 386: 32bit - arm: ARM - arm64: ARM64 - darwin: macOS - linux: Linux - windows: Windows + name_template: >- + {{ .ProjectName }}_ + {{- title .Os }}_ + {{- if eq .Arch "amd64" }}x86_64 + {{- else if eq .Arch "386" }}i386 + {{- else }}{{ .Arch }}{{ end }} + {{- if .Arm }}v{{ .Arm }}{{ end }} checksum: diff --git a/pkg/storage/sqlstorage/migrates/20-optimized-indexes/postgres.sql b/pkg/storage/sqlstorage/migrates/20-optimized-indexes/postgres.sql new file mode 100644 index 000000000..e2535c8e9 --- /dev/null +++ b/pkg/storage/sqlstorage/migrates/20-optimized-indexes/postgres.sql @@ -0,0 +1,8 @@ +--statement +CREATE INDEX IF NOT EXISTS postings_array_length_src ON "VAR_LEDGER_NAME".postings (jsonb_array_length(source)); +--statement +CREATE INDEX IF NOT EXISTS postings_array_length_dst ON "VAR_LEDGER_NAME".postings (jsonb_array_length(destination)); +--statement +CREATE INDEX IF NOT EXISTS accounts_array_length ON "VAR_LEDGER_NAME".accounts (jsonb_array_length(address_json)); +--statement +CREATE INDEX IF NOT EXISTS volumes_array_length ON "VAR_LEDGER_NAME".volumes (jsonb_array_length(account_json)); diff --git a/pkg/storage/sqlstorage/transactions.go b/pkg/storage/sqlstorage/transactions.go index 1a07ae457..e1b8e43c7 100644 --- a/pkg/storage/sqlstorage/transactions.go +++ b/pkg/storage/sqlstorage/transactions.go @@ -36,8 +36,12 @@ func (s *Store) buildTransactionsQuery(flavor Flavor, p ledger.TransactionsQuery metadata = p.Filters.Metadata ) - sb.Select("id", "timestamp", "reference", "metadata", "postings", "pre_commit_volumes", "post_commit_volumes"). - Distinct() + if flavor == SQLite { + sb.Select("id", "timestamp", "reference", "metadata", "postings", "pre_commit_volumes", "post_commit_volumes"). + Distinct() + } else { + sb.Select("distinct on (id) id", "timestamp", "reference", "metadata", "postings", "pre_commit_volumes", "post_commit_volumes") + } sb.From(s.schema.Table("transactions")) if (source != "" || destination != "" || account != "") && flavor == PostgreSQL { // new wildcard handling