Код: Выделить всё
@Query("SELECT es FROM com.cargurus.emailsubscriptions.entity.EmailSubscription es "
+ "JOIN FETCH es.subscriber s "
+ "WHERE es.id > :batchStartId "
+ "AND es.subscriptionTypeId = :typeId "
+ "AND es.active = :active "
+ "ORDER BY es.id ASC")
List findByIdGreaterThanAndSubscriptionTypeIdAndActive(
@Param("batchStartId") long batchStartId,
@Param("typeId") Integer typeId,
@Param("active") Boolean active,
Pageable pageable);
Код: Выделить всё
public class EmailSubscription {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(updatable = false, nullable = false)
private Long id;
@Column(name = "type_id")
private Integer subscriptionTypeId;
private Boolean active;
@Pattern(regexp = LOCALE_VALIDATION_REGEX, message = "invalid code")
private String locale;
private LocalDateTime lastSentTimestamp;
private Integer lastSentSequence;
private Long lastUpdatePersonId;
private LocalDateTime lastUpdateTimestamp;
private LocalDateTime creationTimestamp;
@ManyToOne(targetEntity = Subscriber.class, fetch = FetchType.LAZY, optional = false)
@JoinColumn(name = "subscriber_id", referencedColumnName = "id")
private Subscriber subscriber;
}
Поэтому я написал собственный SQL-запрос, который занимает менее 1 секунды для получения 10 тысяч записей.
Код: Выделить всё
@Query(value = "SELECT es.* FROM subscriptions.email_subscriptions es "
+ "IGNORE INDEX (PRIMARY) "
+ "JOIN subscriptions.subscribers s ON es.subscriber_id = s.id "
+ "WHERE es.type_id = :typeId "
+ "AND es.active = :active "
+ "AND es.id > :batchStartId "
+ "ORDER BY es.id ", nativeQuery = true)
Подробнее здесь: https://stackoverflow.com/questions/790 ... ory-method