我正在使用具有多个子项的Transaction实体进行查询,查询正确返回了结果,但是我的响应时间很慢,要带6条记录与他们的子项进行交易时,会遇到问题3或4秒,我如何才能获得更好的性能,我曾尝试将LazyCollectionOption
设置为false以在急切模式下执行获取,但是它不起作用,查询时间仍然很慢,我如何为此改善响应时间查询?
我正在使用spring data jpa和oracle ojdbc8驱动程序,我从未在一个实体中使用过如此多的一对多关系
交易实体:
@Wither
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode
@Entity
@Table(name = "tbl_psf_transacciones", schema = "schecopa")
@DynamicInsert
public class Transaction extends AuditModel {
@Id
@Column(name = "transaccion_id")
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long transactionId;
/** Some transaction fields **/
@LazyCollection(LazyCollectionOption.EXTRA)
@OneToMany
@JoinColumn(name = "transaccion_id")
private Set<Metadata> metadata = new HashSet<>();
@LazyCollection(LazyCollectionOption.EXTRA)
@OneToMany
@JoinColumn(name = "transaccion_id")
private Set<Movement> movements = new HashSet<>();
@LazyCollection(LazyCollectionOption.EXTRA)
@OneToMany
@JoinColumn(name = "transaccion_id")
private Set<Response> responses = new HashSet<>();
@LazyCollection(LazyCollectionOption.EXTRA)
@OneToMany
@JoinColumn(name = "transaccion_id")
private Set<Amount> amounts = new HashSet<>();
@LazyCollection(LazyCollectionOption.EXTRA)
@OneToMany
@JoinColumn(name = "transaccion_id")
private Set<Tracking> trackings = new HashSet<>();
}
...
事务存储库查询:
public interface TransactionRepository extends JpaRepository<Transaction, Long> {
@Query(value = "select distinct t from Transaction t " +
"join t.movements as movement " +
"on t.transactionId = movement.transaction.transactionId " +
"and movement.customerDocumentType = coalesce(cast(:destinationDocumentType as string), movement.customerDocumentType) " +
"and movement.customerDocumentNumber = coalesce(cast(:detinationDocumentNumber as string), movement.customerDocumentNumber) " +
"and movement.movementType = 'DESTINO' " +
"where t.paymentMethod = coalesce(cast(:paymentMethod as string), t.paymentMethod) " +
"and t.date between :startDate and :endDate " +
"and t.commerceId = coalesce(cast(:idCommerce as string), t.commerceId) " +
"and t.status = coalesce(cast(:idStatus as string), t.status) ")
Page<Transaction> findAllByQueryParams(@Param("paymentMethod") String paymentMethod, @Param("startDate") Date start_date,
@Param("endDate") Date end_date, @Param("destinationDocumentType") String destinationDocumentType,
@Param("detinationDocumentNumber") String destinationDocumentNumber, @Param("idCommerce") String idCommerce,
@Param("idStatus") String idStatus, Pageable pageable);
}
我想改善此查询的响应时间,我已经尝试了一些不同的方法,但是还是一样
更新: 我认为这是一个n + 1选择问题,sql日志显示了很多选择用于该事务数(3万行休眠sql日志) 日志摘要:
2019-07-18 04:54:59.298 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.amounts#101]
2019-07-18 04:54:59.298 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.metadata#101]
2019-07-18 04:54:59.298 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.movements#101]
2019-07-18 04:54:59.298 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.responses#101]
2019-07-18 04:54:59.298 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.trackings#101]
2019-07-18 04:54:59.298 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.amounts#121]
2019-07-18 04:54:59.299 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.metadata#121]
2019-07-18 04:54:59.299 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.movements#121]
2019-07-18 04:54:59.299 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.responses#121]
2019-07-18 04:54:59.299 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.trackings#121]
2019-07-18 04:54:59.299 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.amounts#122]
2019-07-18 04:54:59.299 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.metadata#122]
2019-07-18 04:54:59.299 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.movements#122]
2019-07-18 04:54:59.299 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.responses#122]
2019-07-18 04:54:59.299 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.trackings#122]
2019-07-18 04:54:59.300 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.amounts#123]
2019-07-18 04:54:59.300 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.metadata#123]
2019-07-18 04:54:59.300 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.movements#123]
2019-07-18 04:54:59.300 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.responses#123]
2019-07-18 04:54:59.300 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.trackings#123]
2019-07-18 04:54:59.300 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.amounts#161]
2019-07-18 04:54:59.300 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.metadata#161]
2019-07-18 04:54:59.300 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.movements#161]
2019-07-18 04:54:59.300 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.responses#161]
2019-07-18 04:54:59.300 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.trackings#161]
2019-07-18 04:54:59.301 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.amounts#162]
2019-07-18 04:54:59.301 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.metadata#162]
2019-07-18 04:54:59.301 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.movements#162]
2019-07-18 04:54:59.301 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.responses#162]
2019-07-18 04:54:59.301 TRACE 12435 --- [ task-3] org.hibernate.type.CollectionType : Created collection wrapper: [co.com.bank.transactionservice.model.entity.transaction.Transaction.trackings#162]