Skip to content

Commit

Permalink
fix: index for MAU
Browse files Browse the repository at this point in the history
  • Loading branch information
sattvikc committed Sep 4, 2024
1 parent 331a000 commit e6fb619
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 1 deletion.
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [7.1.3] - 2024-09-04

- Adds index on `last_active_time` for `user_last_active` table to improve the performance of MAU computation.

### Migration

```sql
CREATE INDEX user_last_active_last_active_time_index ON user_last_active (last_active_time DESC, app_id DESC);
```

## [7.1.2] - 2024-08-08

- Fixes tests that check for `Internal Error` in 500 status responses
Expand Down
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ plugins {
id 'java-library'
}

version = "7.1.2"
version = "7.1.3"

repositories {
mavenCentral()
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/io/supertokens/storage/mysql/Start.java
Original file line number Diff line number Diff line change
Expand Up @@ -1300,6 +1300,15 @@ public void updateLastActive(AppIdentifier appIdentifier, String userId) throws
}
}

@TestOnly
public void updateLastActive(AppIdentifier appIdentifier, String userId, long timestamp) throws StorageQueryException {
try {
ActiveUsersQueries.updateUserLastActive(this, appIdentifier, userId, timestamp);
} catch (SQLException e) {
throw new StorageQueryException(e);
}
}

@Override
public int countUsersActiveSince(AppIdentifier appIdentifier, long time) throws StorageQueryException {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
import java.sql.Connection;
import java.sql.SQLException;

import org.jetbrains.annotations.TestOnly;

import static io.supertokens.storage.mysql.QueryExecutorTemplate.execute;
import static io.supertokens.storage.mysql.QueryExecutorTemplate.update;

Expand All @@ -23,6 +25,11 @@ static String getQueryToCreateUserLastActiveTable(Start start) {
+ " );";
}

public 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()
Expand Down Expand Up @@ -110,6 +117,20 @@ public static int updateUserLastActive(Start start, AppIdentifier appIdentifier,
});
}

@TestOnly
public static int updateUserLastActive(Start start, AppIdentifier appIdentifier, String userId, long timestamp)
throws SQLException, StorageQueryException {
String QUERY = "INSERT INTO " + Config.getConfig(start).getUserLastActiveTable()
+ "(app_id, user_id, last_active_time) VALUES(?, ?, ?) ON DUPLICATE KEY UPDATE last_active_time = ?";

return update(start, QUERY, pst -> {
pst.setString(1, appIdentifier.getAppId());
pst.setString(2, userId);
pst.setLong(3, timestamp);
pst.setLong(4, timestamp);
});
}

public static Long getLastActiveByUserId(Start start, AppIdentifier appIdentifier, String userId)
throws StorageQueryException {
String QUERY = "SELECT last_active_time FROM " + Config.getConfig(start).getUserLastActiveTable()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,10 @@ public static void createTablesIfNotExists(Start start, Connection con) throws S
if (!doesTableExists(start, con, Config.getConfig(start).getUserLastActiveTable())) {
getInstance(start).addState(CREATING_NEW_TABLE, null);
update(con, ActiveUsersQueries.getQueryToCreateUserLastActiveTable(start), NO_OP_SETTER);

// index
update(con, ActiveUsersQueries.getQueryToCreateLastActiveTimeIndexForUserLastActiveTable(start),
NO_OP_SETTER);
}

if (!doesTableExists(start, con, Config.getConfig(start).getAccessTokenSigningKeysTable())) {
Expand Down

0 comments on commit e6fb619

Please sign in to comment.