Skip to content

Commit 194eb95

Browse files
refactor: Optimize getUserIdMappingWithEitherSuperTokensUserIdOrExternalUserId query (#243)
Co-authored-by: Sattvik Chakravarthy <sattvik@supertokens.com>
1 parent d5c06aa commit 194eb95

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

CHANGELOG.md

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

1212
- Adds tables and queries for Bulk Import
13+
- Optimize getUserIdMappingWithEitherSuperTokensUserIdOrExternalUserId query
1314

1415
### Migration
1516

@@ -34,6 +35,7 @@ CREATE INDEX IF NOT EXISTS bulk_import_users_pagination_index1 ON bulk_import_us
3435
3536
CREATE INDEX IF NOT EXISTS bulk_import_users_pagination_index2 ON bulk_import_users (app_id, created_at DESC, id DESC);
3637
```
38+
3739
## [7.2.0] - 2024-10-03
3840
3941
- Compatible with plugin interface version 6.3

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

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,12 +139,16 @@ public static UserIdMapping[] getUserIdMappingWithEitherSuperTokensUserIdOrExter
139139
String userId)
140140
throws SQLException, StorageQueryException {
141141
String QUERY = "SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable()
142-
+ " WHERE app_id = ? AND (supertokens_user_id = ? OR external_user_id = ?)";
142+
+ " WHERE app_id = ? AND supertokens_user_id = ?"
143+
+ " UNION ALL "
144+
+ "SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable()
145+
+ " WHERE app_id = ? AND external_user_id = ?";
143146

144147
return execute(start, QUERY, pst -> {
145148
pst.setString(1, appIdentifier.getAppId());
146149
pst.setString(2, userId);
147-
pst.setString(3, userId);
150+
pst.setString(3, appIdentifier.getAppId());
151+
pst.setString(4, userId);
148152
}, result -> {
149153
ArrayList<UserIdMapping> userIdMappingArray = new ArrayList<>();
150154
while (result.next()) {
@@ -375,12 +379,16 @@ public static UserIdMapping[] getUserIdMappingWithEitherSuperTokensUserIdOrExter
375379
String userId)
376380
throws SQLException, StorageQueryException {
377381
String QUERY = "SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable()
378-
+ " WHERE app_id = ? AND (supertokens_user_id = ? OR external_user_id = ?)";
382+
+ " WHERE app_id = ? AND supertokens_user_id = ?"
383+
+ " UNION ALL "
384+
+ "SELECT * FROM " + Config.getConfig(start).getUserIdMappingTable()
385+
+ " WHERE app_id = ? AND external_user_id = ?";
379386

380387
return execute(sqlCon, QUERY, pst -> {
381388
pst.setString(1, appIdentifier.getAppId());
382389
pst.setString(2, userId);
383-
pst.setString(3, userId);
390+
pst.setString(3, appIdentifier.getAppId());
391+
pst.setString(4, userId);
384392
}, result -> {
385393
ArrayList<UserIdMapping> userIdMappingArray = new ArrayList<>();
386394
while (result.next()) {

0 commit comments

Comments
 (0)