I am using Spring Data on top of Hibernate. I have an abstract class A and some implementations, two of which are associated to a large number of records in the database, let's say 200k. I am using a TABLE_PER_CLASS inheritance strategy. Class A has relationship with another class C having two double numbers, d1 and d2. Now, I want to execute a query like findByCD1BetweenAndCD2Between(double minD1, double maxD1, double minD2, double maxD2):
@Repository
public interface ARepository extends
JpaRepository<A, Long>,
JpaSpecificationExecutor<A> {
findByCD1BetweenAndCD2Between(double minD1, double maxD1, double minD2, double maxD2):
}
My class C is embedded and an index is defined:
@Entity
@Configurable
@Embeddable
@Table(indexes = { @Index(columnList = "d1,d2") })
public class C {
private double d1;
private double d2:
}
Class A embeds it:
@Entity
@Inheritance(strategy = InheritanceType.TABLE_PER_CLASS)
@Configurable
public class C {
@Embedded
private Coordinates coordinates;
/*...*/
}
When I do this, the query really takes several minutes. Executing the query directly in SQL does not take that long, as it is a matter of seconds. The number of records matching the condition is in the order of 100. How can I optimize this situation in order to reduce the computation to a few seconds?