在 spring-data-mongdb v3.2.12 中,repository.findAll(pageable) 查询需要 1280ms 才能完成 2M 记录的集合。但使用旧版 spring-data-mongdb v2.2.12.release 版本,在同一集合上只花费了 127 毫秒。 “pageable”参数就像 PageRequest.of(0,100,Sort.by(Direction.ASC,"createTimestamp"))
打开驱动调试日志后,显示spring-data-mongdb v3.2.12首先触发“aggregate”查询,耗时1131ms。但是 spring-data-mongdb v2.2.12.release 首先触发 "count" 查询,只花了 1.34ms。
知道为什么 spring-data-mongdb v3.2.12 在分页中使用非常慢的 “aggregate” 查询吗?
随着 MongoDB 多文档事务的引入,不再可能对计数查询的集合统计信息进行操作。为了获得准确的计数,需要使用参考文档中所述的聚合。 您仍然可以通过在配置中将
MongoTemplate#useEstimatedCount(boolean)
设置为 true
来选择计数估计(如果可能)。