From 31d7d45eabc47a6ab2454a6ad9200a8dcb4ecadb Mon Sep 17 00:00:00 2001 From: Arne Luenser Date: Mon, 19 Jun 2023 15:47:44 +0200 Subject: [PATCH] fix: provide index hint in QueryForCredentials --- persistence/sql/identity/persister_identity.go | 13 ++++++++++++- ...ntifiers_nid_identity_credential_id_idx.down.sql | 1 + ...rs_nid_identity_credential_id_idx.mysql.down.sql | 1 + ...iers_nid_identity_credential_id_idx.mysql.up.sql | 1 + ...dentifiers_nid_identity_credential_id_idx.up.sql | 1 + 5 files changed, 16 insertions(+), 1 deletion(-) create mode 100644 persistence/sql/migrations/sql/20230619000000000001_identity_credential_identifiers_nid_identity_credential_id_idx.down.sql create mode 100644 persistence/sql/migrations/sql/20230619000000000001_identity_credential_identifiers_nid_identity_credential_id_idx.mysql.down.sql create mode 100644 persistence/sql/migrations/sql/20230619000000000001_identity_credential_identifiers_nid_identity_credential_id_idx.mysql.up.sql create mode 100644 persistence/sql/migrations/sql/20230619000000000001_identity_credential_identifiers_nid_identity_credential_id_idx.up.sql diff --git a/persistence/sql/identity/persister_identity.go b/persistence/sql/identity/persister_identity.go index d9e7a983756a..2aaa0ca88a61 100644 --- a/persistence/sql/identity/persister_identity.go +++ b/persistence/sql/identity/persister_identity.go @@ -583,6 +583,17 @@ type Where struct { // QueryForCredentials queries for identity credentials with custom WHERE // clauses, returning the results resolved by the owning identity's UUID. func QueryForCredentials(con *pop.Connection, joinOn string, where ...Where) (map[uuid.UUID](map[identity.CredentialsType]identity.Credentials), error) { + ici := "identity_credential_identifiers" + switch con.Dialect.Name() { + case "cockroach": + ici += "@identity_credential_identifiers_nid_identity_credential_id_idx" + case "sqlite3": + ici += " INDEXED BY identity_credential_identifiers_nid_identity_credential_id_idx" + case "mysql": + ici += " USE INDEX(identity_credential_identifiers_nid_identity_credential_id_idx)" + default: + // good luck 🤷‍♂️ + } q := con.Select( "identity_credentials.id cred_id", "identity_credentials.identity_id identity_id", @@ -598,7 +609,7 @@ func QueryForCredentials(con *pop.Connection, joinOn string, where ...Where) (ma "identity_credential_types ict", "(identity_credentials.identity_credential_type_id = ict.id)", ).LeftJoin( - "identity_credential_identifiers", + ici, joinOn, ) for _, w := range where { diff --git a/persistence/sql/migrations/sql/20230619000000000001_identity_credential_identifiers_nid_identity_credential_id_idx.down.sql b/persistence/sql/migrations/sql/20230619000000000001_identity_credential_identifiers_nid_identity_credential_id_idx.down.sql new file mode 100644 index 000000000000..e95cb5d1f6aa --- /dev/null +++ b/persistence/sql/migrations/sql/20230619000000000001_identity_credential_identifiers_nid_identity_credential_id_idx.down.sql @@ -0,0 +1 @@ +-- nothing diff --git a/persistence/sql/migrations/sql/20230619000000000001_identity_credential_identifiers_nid_identity_credential_id_idx.mysql.down.sql b/persistence/sql/migrations/sql/20230619000000000001_identity_credential_identifiers_nid_identity_credential_id_idx.mysql.down.sql new file mode 100644 index 000000000000..10bbfbe714e3 --- /dev/null +++ b/persistence/sql/migrations/sql/20230619000000000001_identity_credential_identifiers_nid_identity_credential_id_idx.mysql.down.sql @@ -0,0 +1 @@ +-- nothing: needed for foreign key anyways diff --git a/persistence/sql/migrations/sql/20230619000000000001_identity_credential_identifiers_nid_identity_credential_id_idx.mysql.up.sql b/persistence/sql/migrations/sql/20230619000000000001_identity_credential_identifiers_nid_identity_credential_id_idx.mysql.up.sql new file mode 100644 index 000000000000..884196003fc6 --- /dev/null +++ b/persistence/sql/migrations/sql/20230619000000000001_identity_credential_identifiers_nid_identity_credential_id_idx.mysql.up.sql @@ -0,0 +1 @@ +CREATE INDEX identity_credential_identifiers_nid_identity_credential_id_idx ON identity_credential_identifiers (identity_credential_id, nid); diff --git a/persistence/sql/migrations/sql/20230619000000000001_identity_credential_identifiers_nid_identity_credential_id_idx.up.sql b/persistence/sql/migrations/sql/20230619000000000001_identity_credential_identifiers_nid_identity_credential_id_idx.up.sql new file mode 100644 index 000000000000..d89b40baebae --- /dev/null +++ b/persistence/sql/migrations/sql/20230619000000000001_identity_credential_identifiers_nid_identity_credential_id_idx.up.sql @@ -0,0 +1 @@ +-- index already exists