简体   繁体   中英

Java Spring boot Mongodb taking too much time to retrieve results

My MongoDB contains only 3.8k documents and most of the fields are indexed. Moreover the condition I am passing is also indexed. But it's taking ridiculous 5min+ time to fetch those resulted documents which may be 2k in total.

Java Code:

Query query = query(where(DATE_TIME).gte(startDateLocal).andOperator(where(DATE_TIME).lte(endDateLocal)))
        .with(Sort.by(Order.by("_id")));
query.fields().exclude(CarMakerDetailedSpecs);
addDefaultFilter(query);
List<CarMaker> CarMakerItems = mongoTemplate.find(query, CarMaker.class);

return CarMakerItems ;

Resulted Query:

{ "dateTime" : { "$gte" : { "$date" : 1484247600000}}, "$and" : [{ "dateTime" : { "$lte" : { "$date" : 1580324400000}}}], "carMaker" : { "$in" : ["Toyota", "Honda"]}} fields: Document{{carMaker.detailedSpecs=0}}

Interestingly the same query in python is fast enough to get results immediately. So, could there by driver issue? as one other also has complained here as well OR am I missing something? Thanks!

Specs

I am using Java Spring boot 2.2.4.RELEASE, Mongodb.

I don't know what special it is in @Query but surprisingly using it has reduced the time to few seconds.

@Query(value= "{" +
        "  \"dateTime\": {" +
        "    \"$gte\": {" +
        "      \"$date\": ?0" +
        "    }" +
        "  }," +
        "  \"$and\": [" +
        "    {" +
        "      \"dateTime\": {" +
        "        \"$lte\": {" +
        "          \"$date\": ?1" +
        "        }" +
        "      }" +
        "    }" +
        "  ]," +
        "  \"carMaker\": {" +
        "    \"$in\": [" +
        "      \"Toyota\"," +
        "      \"Honda\"" +
        "    ]" +
        "  }" +
        "}",
        fields = 
        "  {" +
        "    carMaker.detailedSpecs=0" +
        "  }" +
        );
List<CarMaker> findAllCarsByStartAndEndDate(LocalDate startDateLocal, LocalDate endDateLocal);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM