From f423a26363cacd654b5b2da4d1ea4eada9a61b20 Mon Sep 17 00:00:00 2001 From: Sattvik Chakravarthy Date: Thu, 14 Sep 2023 13:05:57 +0530 Subject: [PATCH] fix: index updates --- .../inmemorydb/queries/GeneralQueries.java | 45 +++++++++++++------ 1 file changed, 32 insertions(+), 13 deletions(-) diff --git a/src/main/java/io/supertokens/inmemorydb/queries/GeneralQueries.java b/src/main/java/io/supertokens/inmemorydb/queries/GeneralQueries.java index 1282f9ca5..67672989b 100644 --- a/src/main/java/io/supertokens/inmemorydb/queries/GeneralQueries.java +++ b/src/main/java/io/supertokens/inmemorydb/queries/GeneralQueries.java @@ -95,12 +95,27 @@ public static String getQueryToCreateTenantIdIndexForUsersTable(Start start) { + Config.getConfig(start).getUsersTable() + "(app_id, tenant_id);"; } - static String getQueryToCreateUserPaginationIndex(Start start) { - return "CREATE INDEX all_auth_recipe_users_pagination_index ON " + Config.getConfig(start).getUsersTable() - + "(primary_or_recipe_user_time_joined DESC, primary_or_recipe_user_id DESC, tenant_id DESC, app_id DESC);"; + static String getQueryToCreateUserPaginationIndex1(Start start) { + return "CREATE INDEX all_auth_recipe_users_pagination_index1 ON " + Config.getConfig(start).getUsersTable() + + "(app_id, tenant_id, primary_or_recipe_user_time_joined DESC, primary_or_recipe_user_id DESC);"; } - static String getQueryToCreatePrimaryUserIdIndex(Start start) { + static String getQueryToCreateUserPaginationIndex2(Start start) { + return "CREATE INDEX all_auth_recipe_users_pagination_index2 ON " + Config.getConfig(start).getUsersTable() + + "(app_id, tenant_id, primary_or_recipe_user_time_joined ASC, primary_or_recipe_user_id DESC);"; + } + + static String getQueryToCreateUserPaginationIndex3(Start start) { + return "CREATE INDEX all_auth_recipe_users_pagination_index3 ON " + Config.getConfig(start).getUsersTable() + + "(recipe_id, app_id, tenant_id, primary_or_recipe_user_time_joined DESC, primary_or_recipe_user_id DESC);"; + } + + static String getQueryToCreateUserPaginationIndex4(Start start) { + return "CREATE INDEX all_auth_recipe_users_pagination_index4 ON " + Config.getConfig(start).getUsersTable() + + "(recipe_id, app_id, tenant_id, primary_or_recipe_user_time_joined ASC, primary_or_recipe_user_id DESC);"; + } + + static String getQueryToCreatePrimaryUserId(Start start) { /* * Used in: * - does user exist @@ -110,15 +125,14 @@ static String getQueryToCreatePrimaryUserIdIndex(Start start) { + "(app_id, primary_or_recipe_user_id);"; } - static String getQueryToCreatePrimaryUserIdAndTenantIndex(Start start) { + static String getQueryToCreateRecipeIdIndex(Start start) { /* * Used in: * - user count query - * - * */ - return "CREATE INDEX all_auth_recipe_users_primary_user_id_and_tenant_id_index ON " + + * */ + return "CREATE INDEX all_auth_recipe_users_recipe_id_index ON " + Config.getConfig(start).getUsersTable() - + "(app_id, tenant_id, primary_or_recipe_user_id);"; + + "(app_id, recipe_id, tenant_id);"; } private static String getQueryToCreateAppsTable(Start start) { @@ -193,6 +207,8 @@ public static void createTablesIfNotExists(Start start, Main main) throws SQLExc if (!doesTableExists(start, Config.getConfig(start).getKeyValueTable())) { getInstance(main).addState(CREATING_NEW_TABLE, null); update(start, getQueryToCreateKeyValueTable(start), NO_OP_SETTER); + // index + update(start, getQueryToCreateTenantIdIndexForKeyValueTable(start), NO_OP_SETTER); } if (!doesTableExists(start, Config.getConfig(start).getAppIdToUserIdTable())) { @@ -205,9 +221,12 @@ public static void createTablesIfNotExists(Start start, Main main) throws SQLExc update(start, getQueryToCreateUsersTable(start), NO_OP_SETTER); // index - update(start, getQueryToCreatePrimaryUserIdIndex(start), NO_OP_SETTER); - update(start, getQueryToCreateUserPaginationIndex(start), NO_OP_SETTER); - update(start, getQueryToCreatePrimaryUserIdAndTenantIndex(start), NO_OP_SETTER); + update(start, getQueryToCreateUserPaginationIndex1(start), NO_OP_SETTER); + update(start, getQueryToCreateUserPaginationIndex2(start), NO_OP_SETTER); + update(start, getQueryToCreateUserPaginationIndex3(start), NO_OP_SETTER); + update(start, getQueryToCreateUserPaginationIndex4(start), NO_OP_SETTER); + update(start, getQueryToCreatePrimaryUserId(start), NO_OP_SETTER); + update(start, getQueryToCreateRecipeIdIndex(start), NO_OP_SETTER); } if (!doesTableExists(start, Config.getConfig(start).getUserLastActiveTable())) { @@ -237,6 +256,7 @@ public static void createTablesIfNotExists(Start start, Main main) throws SQLExc getInstance(main).addState(CREATING_NEW_TABLE, null); update(start, MultitenancyQueries.getQueryToCreateTenantThirdPartyProvidersTable(start), NO_OP_SETTER); + } if (!doesTableExists(start, Config.getConfig(start).getTenantThirdPartyProviderClientsTable())) { @@ -318,7 +338,6 @@ public static void createTablesIfNotExists(Start start, Main main) throws SQLExc // index update(start, getQueryToCreateCodeCreatedAtIndex(start), NO_OP_SETTER); update(start, getQueryToCreateCodeDeviceIdHashIndex(start), NO_OP_SETTER); - } if (!doesTableExists(start, Config.getConfig(start).getUserMetadataTable())) {