【问题标题】:Slow queries for MongoDB regular expression text in JavaJava中MongoDB正则表达式文本的慢查询
【发布时间】:2020-11-16 15:28:57
【问题描述】:

在MongoDB中搜索正则表达式文本,一开始速度很慢,想知道原因。

只有在JAVA Application Server这个现象上才会发现对应的慢查询。

在 MongoDB shell 中运行相应的查询时,它的运行速度非常快(索引运行良好)。

上述查询中的数据结果值为五个。 集合中的数据总数为 450,000

以下是特定于进程的查询。

=====JAVA进程===== (非常慢 5,518 毫秒)

public List<Contents> findContentList(int rowCnt, long rowNo, String searchContent){
        Query query = new Query();
        query.addCriteria((Criteria.where(DictionaryKey.content).regex("^" + searchContent)));
        if (rowNo > 0) query.addCriteria(Criteria.where(DictionaryKey.contentSeq).gt(rowNo));
        query.with(new Sort(Sort.Direction.ASC, DictionaryKey.contentSeq));
        query.limit(rowCnt);

        return this.mongoTemplate.find(query, Contents.class, Constant.CollectionName.Contents);
       
}  

java监控工具

   Query : Query: { "content" : { "$regex" : "^abcd"}}, Sort: { "contentSeq" : 1}
    Collection Name : contents
    MongTemplate#find() [5,518ms] -- org.springframework.data.mongodb.core.mongTemplate.find()Ljava/util/List;

=====Mongodb Shell====== Mongodb查询(非常快,索引效果很好)

db.contents.found ({content:{"$regex" : "^abcd"}}).sort ({"contentSeq" : 1});

'contents'集合索引是content_1_contentSeq_1

请帮帮我。

【问题讨论】:

  • 见stackoverflow.com/a/56159506/6267549 - 它很慢,因为 Spring 映射到 2 个级别。
  • 监控时,这不是编组问题。从网络获取数据需要很长时间。
  • 如果可能,请发布实际的 Java 代码。
  • @prasad_ 将 Java 代码添加到内容中。
  • Java 代码看起来不错。您是否从mongo shell 访问相同的数据库/数据/集合?

标签: java regex spring mongodb


【解决方案1】:

我找到了原因

原因是某些查询使用了错误的索引。

解决方案是通过提示强制使用索引。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-11-05
    • 2014-08-11
    • 2019-08-29
    • 1970-01-01
    • 2013-07-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多