Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/371.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java中MongoDB正则表达式文本的慢速查询_Java_Regex_Spring_Mongodb - Fatal编程技术网

Java中MongoDB正则表达式文本的慢速查询

Java中MongoDB正则表达式文本的慢速查询,java,regex,spring,mongodb,Java,Regex,Spring,Mongodb,在MongoDB中搜索正则表达式文本时,一开始速度很慢,所以我想知道原因 只有在JAVA应用服务器上才能找到相应的慢速查询 当在MongoDB shell中运行相应的查询时,它工作得非常快(索引工作得很好) 上述查询中的数据结果值数为5。 集合中的数据总数为450000 下面是一个特定于流程的查询 ====JAVA进程===== (非常慢5518ms) ======Mongodb外壳====== Mongodb查询(非常快,索引工作正常) “目录”集合索引为content\u 1\u conte

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

只有在JAVA应用服务器上才能找到相应的慢速查询

当在MongoDB shell中运行相应的查询时,它工作得非常快(索引工作得很好)

上述查询中的数据结果值数为5。 集合中的数据总数为450000

下面是一个特定于流程的查询

====JAVA进程===== (非常慢5518ms)

======Mongodb外壳====== Mongodb查询(非常快,索引工作正常)

“目录”集合索引为
content\u 1\u contentSeq\u 1

请帮帮我。

我找到了原因

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


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

See-这很慢,因为Spring映射了两个级别。在监视时,这不是编组问题。从网络上获取数据花费了很长时间。如果可能,请发布实际的Java代码。@prasad_u将Java代码添加到内容中。Java代码看起来不错。您是否从
mongo
shell访问相同的数据库/数据/集合?
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);
       
}  
   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;
db.contents.found ({content:{"$regex" : "^abcd"}}).sort ({"contentSeq" : 1});