diff --git a/CHANGELOG.md b/CHANGELOG.md index d1d37c974..001af5c22 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,7 +7,25 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html). ## Unreleased -## [9.2.1] - 2024-08-02 +## [9.2.2] - 2024-09-04 + +- Adds index on `last_active_time` for `user_last_active` table to improve the performance of MAU computation. + +### Migration + +If using PostgreSQL, run the following SQL script: + +```sql +CREATE INDEX IF NOT EXISTS user_last_active_last_active_time_index ON user_last_active (last_active_time DESC, app_id DESC); +``` + +If using MySQL, run the following SQL script: + +```sql +CREATE INDEX user_last_active_last_active_time_index ON user_last_active (last_active_time DESC, app_id DESC); +``` + +## [9.2.1] - 2024-09-02 - Removes the stats that were resulting in high CPU consumption diff --git a/build.gradle b/build.gradle index b04ef0fcc..c565f1bdc 100644 --- a/build.gradle +++ b/build.gradle @@ -19,7 +19,7 @@ compileTestJava { options.encoding = "UTF-8" } // } //} -version = "9.2.1" +version = "9.2.2" repositories { diff --git a/src/main/java/io/supertokens/inmemorydb/queries/ActiveUsersQueries.java b/src/main/java/io/supertokens/inmemorydb/queries/ActiveUsersQueries.java index 3daf34809..aa065acf1 100644 --- a/src/main/java/io/supertokens/inmemorydb/queries/ActiveUsersQueries.java +++ b/src/main/java/io/supertokens/inmemorydb/queries/ActiveUsersQueries.java @@ -23,6 +23,11 @@ static String getQueryToCreateUserLastActiveTable(Start start) { + " );"; } + static String getQueryToCreateLastActiveTimeIndexForUserLastActiveTable(Start start) { + return "CREATE INDEX user_last_active_last_active_time_index ON " + + Config.getConfig(start).getUserLastActiveTable() + "(last_active_time DESC, app_id DESC);"; + } + public static int countUsersActiveSince(Start start, AppIdentifier appIdentifier, long sinceTime) throws SQLException, StorageQueryException { String QUERY = "SELECT COUNT(*) as total FROM " + Config.getConfig(start).getUserLastActiveTable() diff --git a/src/main/java/io/supertokens/inmemorydb/queries/GeneralQueries.java b/src/main/java/io/supertokens/inmemorydb/queries/GeneralQueries.java index 57b11eafd..edf7a7f11 100644 --- a/src/main/java/io/supertokens/inmemorydb/queries/GeneralQueries.java +++ b/src/main/java/io/supertokens/inmemorydb/queries/GeneralQueries.java @@ -245,6 +245,10 @@ public static void createTablesIfNotExists(Start start, Main main) throws SQLExc if (!doesTableExists(start, Config.getConfig(start).getUserLastActiveTable())) { getInstance(main).addState(CREATING_NEW_TABLE, null); update(start, ActiveUsersQueries.getQueryToCreateUserLastActiveTable(start), NO_OP_SETTER); + + // index + update(start, ActiveUsersQueries.getQueryToCreateLastActiveTimeIndexForUserLastActiveTable(start), + NO_OP_SETTER); } if (!doesTableExists(start, Config.getConfig(start).getAccessTokenSigningKeysTable())) {