我正在尝试加快查询速度:
@Query("select max(t.timestamp) from TradeDbo t where t.currencyPair.id = :currencyPairId")
Date findMaxTimestamp(@Param("currencyPairId") Long currencyPairId);
该实体的定义如下:
@Entity()
@Table(name = "Trade",
indexes = { @Index(name = "idx_timestamp_currencypairid",
columnList = "timestamp,currency_pair_id")})
public class TradeDbo extends Auditable<String> {
@Id @GeneratedValue
@Getter private Long id;
@Version
private long version;
@JoinColumn(name = "currency_pair_id")
@ManyToOne(fetch=FetchType.EAGER)
@Getter private CurrencyPairDbo currencyPair;
@Column(name = "timestamp")
@Convert(converter = DateConverter.class)
@Getter private Date timestamp;
...
,如您所见,我已经在timestamp / currencypairid上定义了一个索引(请参阅how to speed up max() query),我认为这会使max(timestamp)只是读取btree的最后一页,但仍然需要和添加索引之前一样长的时间。
答案 0 :(得分:2)
以
进行查询"select max(t.timestamp) from TradeDbo t where t.currencyPair_id = :currencyPairId"
您的综合索引对您的性能没有帮助,您应该更改列顺序
columnList = "currency_pair_id,timestamp"
首先使用与where条件..
有关的列使用列构建的索引内容用于从左到右阅读