Skip to content

Commit

Permalink
refactor: add query hints to improve performance in AnalyticsRepository
Browse files Browse the repository at this point in the history
  • Loading branch information
LeonardoMeireles55 committed Feb 3, 2025
1 parent d6b1c81 commit c47ccf1
Showing 1 changed file with 9 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package leonardo.labutilities.qualitylabpro.repositories;

import jakarta.persistence.QueryHint;
import jakarta.transaction.Transactional;
import leonardo.labutilities.qualitylabpro.dtos.analytics.AnalyticsDTO;
import leonardo.labutilities.qualitylabpro.entities.Analytic;
Expand All @@ -8,6 +9,7 @@
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.jpa.repository.QueryHints;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

Expand Down Expand Up @@ -63,6 +65,9 @@ List<Analytic> findByNameAndLevel(Pageable pageable, @Param("name") String name,
List<Analytic> findByNameAndLevelAndLevelLot(Pageable pageable, @Param("name") String name,
@Param("level") String level, @Param("levelLot") String levelLot);

@QueryHints({@QueryHint(name = "org.hibernate.readOnly", value = "true"),
@QueryHint(name = "org.hibernate.fetchSize", value = "50"),
@QueryHint(name = "org.hibernate.cacheable", value = "true")})
@Query("""
SELECT ga FROM generic_analytics ga WHERE ga.name = :name
AND ga.level = :level AND ga.date BETWEEN :startDate AND :endDate ORDER BY ga.date ASC
Expand All @@ -72,6 +77,10 @@ List<Analytic> findByNameAndLevelAndDateBetween(@Param("name") String name,
@Param("endDate") LocalDateTime endDate, Pageable pageable);

// Fetch Analytics by Multiple Names and Date

@QueryHints({@QueryHint(name = "org.hibernate.readOnly", value = "true"),
@QueryHint(name = "org.hibernate.fetchSize", value = "50"),
@QueryHint(name = "org.hibernate.cacheable", value = "true")})
@Query(value = """
SELECT ga FROM generic_analytics ga WHERE
ga.name IN (:names) AND ga.level = :level AND ga.date BETWEEN
Expand Down

0 comments on commit c47ccf1

Please sign in to comment.