簡體   English   中英

正確使用@CompoundIndex 和 MongoRepository

[英]Correct use of @CompoundIndex and MongoRepository

我的查詢響應很慢,所以我想知道我是否正在創建復合 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);
        
         
}

問候康泰

如何驗證索引是否有效工作。

這里有兩種方式...

  1. 通過負載測試覆蓋您的代碼。 運行一個沒有索引但有索引的版本。 假設在那里插入 1000 萬條記錄並嘗試進行一些讀取。 按照本指南設置集成測試,然后計時。
  2. 您可以添加索引,然后按照本指南閱讀查詢計划。 它會讓你知道你的索引是否會被使用。

所以你去......一個實驗和一個理論解決方案。 我建議探索兩者。 為您將來解決此類問題提供良好的背景。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM