【发布时间】:2021-12-24 17:38:36
【问题描述】:
我的查询响应速度很慢,所以我想知道我是否正在创建复合 mongo 索引并在 MongoRepository 中有效地使用它,以及如何验证索引是否有效。
@CompoundIndexes({
@CompoundIndex(name = "onwebsite_process_sequence", def = "{'additionalfields.onwebsite': 1, 'additionalfields.process' : 1, 'additionalfields.onwebsite': 1}")
})
public class DdsJson {
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("additional_fields")
AdditionalFields additionalfields = new AdditionalFields();
...
}
public class AdditionalFields {
public static final Logger LOG = LoggerFactory.getLogger(AdditionalFields.class);
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("sequence")
@Indexed
private Integer sequence;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("process")
@Indexed
private Integer process;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
@JsonProperty("onwebsite")
@Indexed
private String onwebsite;
}
public interface DdsJsonRepository extends MongoRepository<DdsJson, Integer> {
Page<DdsJson> findByAdditionalfields_OnwebsiteAndAdditionalfields_SequenceAndAdditionalfields_Sequence(String onwebsite,int process, int sequence, Pageable pageable);
}
问候 联系方式
【问题讨论】:
-
您可以通过在查询上运行
explain方法来验证是否正在使用索引。
标签: java mongodb spring-boot