diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/DatabaseChangelog.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/DatabaseChangelog.java index e7269d86a9..d9ce643c7e 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/DatabaseChangelog.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/DatabaseChangelog.java @@ -187,7 +187,7 @@ public class DatabaseChangelog { * Also, please check out the following blog on how to best create indexes : * https://emptysqua.re/blog/optimizing-mongodb-compound-indexes/ */ - private static Index makeIndex(String... fields) { + public static Index makeIndex(String... fields) { if (fields.length == 1) { return new Index(fields[0], Sort.Direction.ASC).named(fields[0]); } else { @@ -203,14 +203,14 @@ public class DatabaseChangelog { * Given a MongockTemplate, a domain class and a bunch of Index definitions, this pure utility function will ensure * those indexes on the database behind the MongockTemplate instance. */ - private static void ensureIndexes(MongockTemplate mongoTemplate, Class entityClass, Index... indexes) { + public static void ensureIndexes(MongockTemplate mongoTemplate, Class entityClass, Index... indexes) { IndexOperations indexOps = mongoTemplate.indexOps(entityClass); for (Index index : indexes) { indexOps.ensureIndex(index); } } - private static void dropIndexIfExists(MongockTemplate mongoTemplate, Class entityClass, String name) { + public static void dropIndexIfExists(MongockTemplate mongoTemplate, Class entityClass, String name) { try { mongoTemplate.indexOps(entityClass).dropIndex(name); } catch (UncategorizedMongoDbException ignored) { diff --git a/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/DatabaseChangelog2.java b/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/DatabaseChangelog2.java index 25f6a67ac5..033c427186 100644 --- a/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/DatabaseChangelog2.java +++ b/app/server/appsmith-server/src/main/java/com/appsmith/server/migrations/DatabaseChangelog2.java @@ -2,7 +2,10 @@ package com.appsmith.server.migrations; import com.appsmith.external.models.Datasource; import com.appsmith.external.models.Property; +import com.appsmith.external.models.QBaseDomain; import com.appsmith.external.models.QDatasource; +import com.appsmith.server.constants.FieldName; +import com.appsmith.server.domains.ActionCollection; import com.appsmith.server.domains.Application; import com.appsmith.server.domains.ApplicationPage; import com.appsmith.server.domains.NewAction; @@ -35,6 +38,9 @@ import java.util.regex.Matcher; import java.util.regex.Pattern; import java.util.stream.Collectors; +import static com.appsmith.server.migrations.DatabaseChangelog.dropIndexIfExists; +import static com.appsmith.server.migrations.DatabaseChangelog.ensureIndexes; +import static com.appsmith.server.migrations.DatabaseChangelog.makeIndex; import static com.appsmith.server.repositories.BaseAppsmithRepositoryImpl.fieldName; import static java.lang.Boolean.TRUE; import static org.springframework.data.mongodb.core.query.Criteria.where; @@ -701,4 +707,43 @@ public class DatabaseChangelog2 { } } + /** + * This migration introduces indexes on newAction, actionCollection, newPage to improve the query performance for + * queries like getResourceByDefaultAppIdAndGitSyncId which excludes the deleted entries. + */ + @ChangeSet(order = "007", id = "update-git-indexes", author = "") + public void addIndexesForGit(MongockTemplate mongockTemplate) { + + dropIndexIfExists(mongockTemplate, NewAction.class, "defaultApplicationId_gitSyncId_compound_index"); + dropIndexIfExists(mongockTemplate, ActionCollection.class, "defaultApplicationId_gitSyncId_compound_index"); + + String defaultResources = fieldName(QBaseDomain.baseDomain.defaultResources); + ensureIndexes(mongockTemplate, ActionCollection.class, + makeIndex( + defaultResources + "." + FieldName.APPLICATION_ID, + fieldName(QBaseDomain.baseDomain.gitSyncId), + fieldName(QBaseDomain.baseDomain.deleted) + ) + .named("defaultApplicationId_gitSyncId_deleted_compound_index") + ); + + ensureIndexes(mongockTemplate, NewAction.class, + makeIndex( + defaultResources + "." + FieldName.APPLICATION_ID, + fieldName(QBaseDomain.baseDomain.gitSyncId), + fieldName(QBaseDomain.baseDomain.deleted) + ) + .named("defaultApplicationId_gitSyncId_deleted_compound_index") + ); + + ensureIndexes(mongockTemplate, NewPage.class, + makeIndex( + defaultResources + "." + FieldName.APPLICATION_ID, + fieldName(QBaseDomain.baseDomain.gitSyncId), + fieldName(QBaseDomain.baseDomain.deleted) + ) + .named("defaultApplicationId_gitSyncId_deleted_compound_index") + ); + } + }