Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add PG indexes #4230

Merged
merged 4 commits into from
Jul 23, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,9 @@
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Index;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import java.io.Serial;
import java.util.UUID;
Expand All @@ -21,6 +23,15 @@
@Accessors(chain = true)
@TSModel
@Entity
@Table(
name = "project_asset",
indexes = {
@Index(name = "idx_asset_id", columnList = "assetId"),
@Index(name = "idx_asset_type", columnList = "assetType"),
@Index(name = "idx_project_id", columnList = "project_id"),
@Index(name = "idx_project_asset_count", columnList = "project_id, assetType, deletedOn")
}
)
public class ProjectAsset extends TerariumAsset {

@Serial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,9 @@
import jakarta.persistence.Entity;
import jakarta.persistence.EnumType;
import jakarta.persistence.Enumerated;
import jakarta.persistence.Index;
import jakarta.persistence.ManyToOne;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import java.io.Serial;
import java.sql.Timestamp;
Expand All @@ -26,6 +28,10 @@
@EqualsAndHashCode(callSuper = true)
@TSModel
@Entity
@Table(
name = "notification_event",
indexes = { @Index(name = "idx_notification_group_id", columnList = "notification_group_id") }
)
public class NotificationEvent extends TerariumEntity {

@Serial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
import jakarta.persistence.CascadeType;
import jakarta.persistence.Entity;
import jakarta.persistence.FetchType;
import jakarta.persistence.Index;
import jakarta.persistence.OneToMany;
import jakarta.persistence.OrderBy;
import jakarta.persistence.Table;
import jakarta.validation.constraints.NotNull;
import java.io.Serial;
import java.util.ArrayList;
Expand All @@ -24,6 +26,12 @@
@EqualsAndHashCode(callSuper = true)
@TSModel
@Entity
@Table(
name = "notification_group",
indexes = {
@Index(name = "idx_user_id", columnList = "userId"), @Index(name = "idx_created_on", columnList = "createdOn")
}
)
public class NotificationGroup extends TerariumEntity {

@Serial
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.repository.NoRepositoryBean;
import software.uncharted.terarium.hmiserver.models.dataservice.model.ModelFramework;

@NoRepositoryBean
public interface PSCrudSoftDeleteRepository<T, ID> extends PSCrudRepository<T, ID> {
Expand All @@ -13,4 +14,6 @@ public interface PSCrudSoftDeleteRepository<T, ID> extends PSCrudRepository<T, I
Optional<T> getByIdAndDeletedOnIsNull(final ID id);

Page<T> findAllByPublicAssetIsTrueAndTemporaryIsFalseAndDeletedOnIsNull(final Pageable pageable);

List<ModelFramework> findAllByDeletedOnIsNull();
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,9 @@
package software.uncharted.terarium.hmiserver.repository.data;

import java.util.List;
import java.util.Optional;
import java.util.UUID;
import org.springframework.stereotype.Repository;
import software.uncharted.terarium.hmiserver.models.dataservice.model.ModelFramework;
import software.uncharted.terarium.hmiserver.repository.PSCrudRepository;

@Repository
public interface FrameworkRepository extends PSCrudRepository<ModelFramework, UUID> {
List<ModelFramework> findAllByDeletedOnIsNull();

List<ModelFramework> findAllByIdInAndDeletedOnIsNull(final List<UUID> ids);

Optional<ModelFramework> getByIdAndDeletedOnIsNull(final UUID id);
}
public interface FrameworkRepository extends PSCrudRepository<ModelFramework, UUID> {}
Original file line number Diff line number Diff line change
Expand Up @@ -18,38 +18,39 @@ public interface ProjectRepository extends PSCrudRepository<Project, UUID>, JpaS
Optional<Project> getByIdAndDeletedOnIsNull(final UUID id);

@Query(
"select " +
" p.id as id, " +
" p.createdOn as createdOn, " +
" p.updatedOn as updatedOn, " +
" p.deletedOn as deletedOn, " +
" p.description as description, " +
" p.fileNames as fileNames, " +
" p.name as name, " +
" p.overviewContent as overviewContent, " +
" p.publicAsset as publicAsset, " +
" p.temporary as temporary, " +
" p.thumbnail as thumbnail, " +
" p.userId as userId, " +
" p2.assetCount as assetCount, " +
" p2.assetType as assetType " +
"from " +
" Project p " +
"left join (" +
"select " +
" pa.project.id as projectId, " +
" pa.assetType as assetType, " +
" count(*) as assetCount " +
"from " +
" ProjectAsset pa " +
"where " +
" pa.deletedOn is null " +
"group by pa.project.id, pa.assetType " +
") as p2 " +
"on p.id = p2.projectId " +
"where " +
" p.id in (:ids) " +
" and p.deletedOn is null"
"""
select
p.id as id,
p.createdOn as createdOn,
p.updatedOn as updatedOn,
p.deletedOn as deletedOn,
p.description as description,
p.fileNames as fileNames,
p.name as name,
p.overviewContent as overviewContent,
p.publicAsset as publicAsset,
p.temporary as temporary,
p.thumbnail as thumbnail,
p.userId as userId,
p2.assetCount as assetCount,
p2.assetType as assetType
from
Project p
left join (
select
pa.project.id as projectId,
pa.assetType as assetType,
count(*) as assetCount
from
ProjectAsset pa
where
pa.deletedOn is null
group by pa.project.id, pa.assetType) as p2
on p.id = p2.projectId
where
p.id in (:ids)
and p.deletedOn is null
"""
)
List<ProjectAndAssetAggregate> findByIdsWithAssets(@Param("ids") final List<UUID> ids);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@

-- Remove this, due to flyway baseline, we need indexes created in the code / entity
DROP INDEX IF EXISTS pa_count;
Loading