【问题标题】:Correct use of @CompoundIndex and MongoRepository正确使用@CompoundIndex 和 MongoRepository
【发布时间】: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


【解决方案1】:

如何验证索引是否有效。

这里有两种方式...

  1. 通过负载测试覆盖您的代码。运行不带索引和带索引的版本。假设在其中插入 1000 万条记录并尝试进行一些读取。按照这个guide 设置集成测试,然后计时。
  2. 您可以添加索引,然后阅读此guide 之后的查询计划。它可以让您知道您的索引是否会被使用。

所以你去...一个实验和理论解决方案。我建议两者都探索。为您将来解决此类问题提供良好的背景。

【讨论】:

    猜你喜欢
    • 2021-02-15
    • 1970-01-01
    • 2017-05-05
    • 1970-01-01
    • 2022-11-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-16
    相关资源
    最近更新 更多