diff --git a/bootstrap/sql/migrations/native/1.4.4/mysql/schemaChanges.sql b/bootstrap/sql/migrations/native/1.4.4/mysql/schemaChanges.sql index f8e00d93e7f..d4153e47327 100644 --- a/bootstrap/sql/migrations/native/1.4.4/mysql/schemaChanges.sql +++ b/bootstrap/sql/migrations/native/1.4.4/mysql/schemaChanges.sql @@ -1 +1,13 @@ -ALTER TABLE user_entity ADD UNIQUE (name); \ No newline at end of file +ALTER TABLE user_entity ADD UNIQUE (name); + +-- Adding a new column 'taskAssigneesIds' to improve query performance by avoiding the use of JSON_EXTRACT in queries +-- This column is generated to store the 'id' values extracted from the 'taskAssignees' JSON array, which helps in optimizing search and filtering operations. +ALTER TABLE thread_entity +ADD COLUMN taskAssigneesIds TEXT GENERATED ALWAYS AS (JSON_EXTRACT(taskAssignees, '$[*].id')); +ALTER TABLE thread_entity ADD INDEX taskAssigneesIds_index (taskAssigneesIds(700)); + +-- Creating an index on (type, resolved, updatedAt) to optimize query performance for filtering by these columns for activity feed.-FeedDao.List +CREATE INDEX thread_type_resolved_updatedAt_index ON thread_entity (type, resolved, updatedAt); + +-- Creating an index on (createdAt) to improve performance for queries that sort or filter by creation date in activity feed.-FeedDao.List +CREATE INDEX created_at_index ON thread_entity (createdAt); diff --git a/bootstrap/sql/migrations/native/1.4.4/postgres/schemaChanges.sql b/bootstrap/sql/migrations/native/1.4.4/postgres/schemaChanges.sql index f8e00d93e7f..6c4f79b074f 100644 --- a/bootstrap/sql/migrations/native/1.4.4/postgres/schemaChanges.sql +++ b/bootstrap/sql/migrations/native/1.4.4/postgres/schemaChanges.sql @@ -1 +1,18 @@ -ALTER TABLE user_entity ADD UNIQUE (name); \ No newline at end of file +ALTER TABLE user_entity ADD UNIQUE (name); + +-- Adding a new column 'taskAssigneesIds' to improve query performance by avoiding the use of JSON_EXTRACT in queries +-- This column is generated to store the 'id' values extracted from the 'taskAssignees' JSON array, which helps in optimizing search and filtering operations. +ALTER TABLE thread_entity +ADD COLUMN taskAssigneesIds TEXT; +UPDATE thread_entity +SET taskAssigneesIds = ( + SELECT jsonb_agg(elem->>'id') + FROM jsonb_array_elements(taskAssignees) AS elem +); +CREATE INDEX taskAssigneesIds_index ON thread_entity(taskAssigneesIds); + +-- Creating an index on (type, resolved, updatedAt) to optimize query performance for filtering by these columns for activity feed.-FeedDao.List +CREATE INDEX thread_type_resolved_updatedAt_index ON thread_entity (type, resolved, updatedAt); + +-- Creating an index on (createdAt) to improve performance for queries that sort or filter by creation date in activity feed.-FeedDao.List +CREATE INDEX created_at_index ON thread_entity (createdAt); \ No newline at end of file diff --git a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/CollectionDAO.java b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/CollectionDAO.java index f90bb9bf11f..4c067cf3ead 100644 --- a/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/CollectionDAO.java +++ b/openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/CollectionDAO.java @@ -1012,7 +1012,7 @@ public interface CollectionDAO { @ConnectionAwareSqlQuery( value = "SELECT json FROM thread_entity AND " - + "JSON_OVERLAPS(json_extract(taskAssignees, '$[*].id'), :userTeamJsonMysql) " + + "JSON_OVERLAPS(taskAssigneesIds, :userTeamJsonMysql) " + "ORDER BY createdAt DESC " + "LIMIT :limit", connectionType = MYSQL) @@ -1030,7 +1030,7 @@ public interface CollectionDAO { @ConnectionAwareSqlQuery( value = "SELECT count(id) FROM thread_entity AND " - + "JSON_OVERLAPS(json_extract(taskAssignees, '$[*].id'), :userTeamJsonMysql) ", + + "JSON_OVERLAPS(taskAssigneesIds, :userTeamJsonMysql) ", connectionType = MYSQL) int listCountTasksAssignedTo( @BindList("userTeamJsonPostgres") List userTeamJsonPostgres, @@ -1047,7 +1047,7 @@ public interface CollectionDAO { @ConnectionAwareSqlQuery( value = "SELECT json FROM thread_entity " - + "AND (JSON_OVERLAPS(JSON_EXTRACT(taskAssignees, '$[*].id'), :userTeamJsonMysql) OR createdBy = :username) " + + "AND (JSON_OVERLAPS(taskAssigneesIds, :userTeamJsonMysql) OR createdBy = :username) " + "ORDER BY createdAt DESC " + "LIMIT :limit", connectionType = MYSQL) @@ -1066,7 +1066,7 @@ public interface CollectionDAO { @ConnectionAwareSqlQuery( value = "SELECT count(id) FROM thread_entity " - + "AND (JSON_OVERLAPS(JSON_EXTRACT(taskAssignees, '$[*].id'), :userTeamJsonMysql) OR createdBy = :username) ", + + "AND (JSON_OVERLAPS(taskAssigneesIds, :userTeamJsonMysql) OR createdBy = :username) ", connectionType = MYSQL) int listCountTasksOfUser( @BindList("userTeamJsonPostgres") List userTeamJsonPostgres, @@ -1245,7 +1245,7 @@ public interface CollectionDAO { + "id in (SELECT toId FROM entity_relationship WHERE (fromEntity='user' AND fromId= :userId AND toEntity='THREAD' AND relation IN (1,2))) " + " OR id in (SELECT toId FROM entity_relationship WHERE ((fromEntity='user' AND fromId= :userId) OR " + "(fromEntity='team' AND fromId IN ())) AND relation=11)) OR " - + "(JSON_OVERLAPS(JSON_EXTRACT(taskAssignees, '$[*].id'), :userTeamJsonMysql) OR createdBy = :username)" + + "(JSON_OVERLAPS(taskAssigneesIds, :userTeamJsonMysql) OR createdBy = :username)" + " GROUP BY te.type, te.taskStatus", connectionType = MYSQL) @RegisterRowMapper(OwnerCountFieldMapper.class)