Skip to content

Commit

Permalink
fix: slow query fix (#223)
Browse files Browse the repository at this point in the history
* fix: slow query fix

* fix: version and changelog
  • Loading branch information
sattvikc authored Sep 2, 2024
1 parent 60b6e0f commit 0d115f2
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

## [7.1.2] - 2024-09-02

- Optimizes users count query

## [7.1.1] - 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.1"
version = "7.1.2"

repositories {
mavenCentral()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -705,8 +705,8 @@ public static void deleteKeyValue_Transaction(Start start, Connection con, Tenan
public static long getUsersCount(Start start, AppIdentifier appIdentifier, RECIPE_ID[] includeRecipeIds)
throws SQLException, StorageQueryException {
StringBuilder QUERY = new StringBuilder(
"SELECT COUNT(DISTINCT primary_or_recipe_user_id) AS total FROM " +
getConfig(start).getUsersTable());
"SELECT COUNT(*) AS total FROM (");
QUERY.append("SELECT primary_or_recipe_user_id FROM " + getConfig(start).getUsersTable());
QUERY.append(" WHERE app_id = ?");
if (includeRecipeIds != null && includeRecipeIds.length > 0) {
QUERY.append(" AND recipe_id IN (");
Expand All @@ -719,6 +719,7 @@ public static long getUsersCount(Start start, AppIdentifier appIdentifier, RECIP
}
QUERY.append(")");
}
QUERY.append(" GROUP BY primary_or_recipe_user_id) AS uniq_users");

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

QUERY.append(" GROUP BY primary_or_recipe_user_id) AS uniq_users");

return execute(start, QUERY.toString(), pst -> {
pst.setString(1, tenantIdentifier.getAppId());
pst.setString(2, tenantIdentifier.getTenantId());
Expand Down

0 comments on commit 0d115f2

Please sign in to comment.