From 99b4fd0bc24c4a7ac699337e5c6f7869827e96be Mon Sep 17 00:00:00 2001 From: Silvan Date: Mon, 8 Jul 2024 17:54:19 +0200 Subject: [PATCH] fix(fields): add index to improve search by aggregate (#8267) # Which Problems Are Solved During performance testing of the `eventstore.fields` table we found some long running queries which searched for the aggregate id. # How the Problems Are Solved A new index was added to the `eventstore.fields`-table called `f_aggregate_object_type_idx`. # Additional Changes None # Additional Context - Table was added in https://github.com/zitadel/zitadel/pull/8191 - Part of https://github.com/zitadel/zitadel/issues/7639 (cherry picked from commit 23bebc7e30ebd6a20dd0a87480863a5557d5b3fc) --- cmd/setup/31.go | 27 +++++++++++++++++++++++++++ cmd/setup/31.sql | 1 + cmd/setup/config.go | 1 + cmd/setup/setup.go | 2 ++ 4 files changed, 31 insertions(+) create mode 100644 cmd/setup/31.go create mode 100644 cmd/setup/31.sql diff --git a/cmd/setup/31.go b/cmd/setup/31.go new file mode 100644 index 0000000000..640c11b1c0 --- /dev/null +++ b/cmd/setup/31.go @@ -0,0 +1,27 @@ +package setup + +import ( + "context" + _ "embed" + + "github.com/zitadel/zitadel/internal/database" + "github.com/zitadel/zitadel/internal/eventstore" +) + +var ( + //go:embed 31.sql + addAggregateIndexToFields string +) + +type AddAggregateIndexToFields struct { + dbClient *database.DB +} + +func (mig *AddAggregateIndexToFields) Execute(ctx context.Context, _ eventstore.Event) error { + _, err := mig.dbClient.ExecContext(ctx, addAggregateIndexToFields) + return err +} + +func (mig *AddAggregateIndexToFields) String() string { + return "31_add_aggregate_index_to_fields" +} diff --git a/cmd/setup/31.sql b/cmd/setup/31.sql new file mode 100644 index 0000000000..4198f2326e --- /dev/null +++ b/cmd/setup/31.sql @@ -0,0 +1 @@ +CREATE INDEX CONCURRENTLY IF NOT EXISTS f_aggregate_object_type_idx ON eventstore.fields (aggregate_type, aggregate_id, object_type); \ No newline at end of file diff --git a/cmd/setup/config.go b/cmd/setup/config.go index 2f4f47c768..2bee4642aa 100644 --- a/cmd/setup/config.go +++ b/cmd/setup/config.go @@ -114,6 +114,7 @@ type Steps struct { s28AddFieldTable *AddFieldTable s29FillFieldsForProjectGrant *FillFieldsForProjectGrant s30FillFieldsForOrgDomainVerified *FillFieldsForOrgDomainVerified + s31AddAggregateIndexToFields *AddAggregateIndexToFields } func MustNewSteps(v *viper.Viper) *Steps { diff --git a/cmd/setup/setup.go b/cmd/setup/setup.go index a36ccfbe0c..f9a40697a2 100644 --- a/cmd/setup/setup.go +++ b/cmd/setup/setup.go @@ -159,6 +159,7 @@ func Setup(ctx context.Context, config *Config, steps *Steps, masterKey string) steps.s28AddFieldTable = &AddFieldTable{dbClient: esPusherDBClient} steps.s29FillFieldsForProjectGrant = &FillFieldsForProjectGrant{eventstore: eventstoreClient} steps.s30FillFieldsForOrgDomainVerified = &FillFieldsForOrgDomainVerified{eventstore: eventstoreClient} + steps.s31AddAggregateIndexToFields = &AddAggregateIndexToFields{dbClient: esPusherDBClient} err = projection.Create(ctx, projectionDBClient, eventstoreClient, config.Projections, nil, nil, nil) logging.OnError(err).Fatal("unable to start projections") @@ -182,6 +183,7 @@ func Setup(ctx context.Context, config *Config, steps *Steps, masterKey string) steps.s1ProjectionTable, steps.s2AssetsTable, steps.s28AddFieldTable, + steps.s31AddAggregateIndexToFields, steps.FirstInstance, steps.s5LastFailed, steps.s6OwnerRemoveColumns,