Skip to content

Commit 0d115f2

Browse files
authored
fix: slow query fix (#223)
* fix: slow query fix * fix: version and changelog
1 parent 60b6e0f commit 0d115f2

File tree

3 files changed

+12
-4
lines changed

3 files changed

+12
-4
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

88
## [Unreleased]
99

10+
## [7.1.2] - 2024-09-02
11+
12+
- Optimizes users count query
13+
1014
## [7.1.1] - 2024-08-08
1115

1216
- Fixes tests that check for `Internal Error` in 500 status responses

build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ plugins {
22
id 'java-library'
33
}
44

5-
version = "7.1.1"
5+
version = "7.1.2"
66

77
repositories {
88
mavenCentral()

src/main/java/io/supertokens/storage/postgresql/queries/GeneralQueries.java

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -705,8 +705,8 @@ public static void deleteKeyValue_Transaction(Start start, Connection con, Tenan
705705
public static long getUsersCount(Start start, AppIdentifier appIdentifier, RECIPE_ID[] includeRecipeIds)
706706
throws SQLException, StorageQueryException {
707707
StringBuilder QUERY = new StringBuilder(
708-
"SELECT COUNT(DISTINCT primary_or_recipe_user_id) AS total FROM " +
709-
getConfig(start).getUsersTable());
708+
"SELECT COUNT(*) AS total FROM (");
709+
QUERY.append("SELECT primary_or_recipe_user_id FROM " + getConfig(start).getUsersTable());
710710
QUERY.append(" WHERE app_id = ?");
711711
if (includeRecipeIds != null && includeRecipeIds.length > 0) {
712712
QUERY.append(" AND recipe_id IN (");
@@ -719,6 +719,7 @@ public static long getUsersCount(Start start, AppIdentifier appIdentifier, RECIP
719719
}
720720
QUERY.append(")");
721721
}
722+
QUERY.append(" GROUP BY primary_or_recipe_user_id) AS uniq_users");
722723

723724
return execute(start, QUERY.toString(), pst -> {
724725
pst.setString(1, appIdentifier.getAppId());
@@ -739,7 +740,8 @@ public static long getUsersCount(Start start, AppIdentifier appIdentifier, RECIP
739740
public static long getUsersCount(Start start, TenantIdentifier tenantIdentifier, RECIPE_ID[] includeRecipeIds)
740741
throws SQLException, StorageQueryException {
741742
StringBuilder QUERY = new StringBuilder(
742-
"SELECT COUNT(DISTINCT primary_or_recipe_user_id) AS total FROM " + getConfig(start).getUsersTable());
743+
"SELECT COUNT(*) AS total FROM (");
744+
QUERY.append("SELECT primary_or_recipe_user_id FROM " + getConfig(start).getUsersTable());
743745
QUERY.append(" WHERE app_id = ? AND tenant_id = ?");
744746
if (includeRecipeIds != null && includeRecipeIds.length > 0) {
745747
QUERY.append(" AND recipe_id IN (");
@@ -753,6 +755,8 @@ public static long getUsersCount(Start start, TenantIdentifier tenantIdentifier,
753755
QUERY.append(")");
754756
}
755757

758+
QUERY.append(" GROUP BY primary_or_recipe_user_id) AS uniq_users");
759+
756760
return execute(start, QUERY.toString(), pst -> {
757761
pst.setString(1, tenantIdentifier.getAppId());
758762
pst.setString(2, tenantIdentifier.getTenantId());

0 commit comments

Comments
 (0)