【发布时间】:2013-07-04 08:12:39
【问题描述】:
我使用 Spring 中的 MongoTemplate 来访问 MongoDB。
final Query query = new Query(Criteria.where("_id").exists(true));
query.with(new Sort(Direction.ASC, "FIRSTNAME", "LASTNAME", "EMAIL"));
if (count > 0) {
query.limit(count);
}
query.skip(start);
query.fields().include("FIRSTNAME");
query.fields().include("LASTNAME");
query.fields().include("EMAIL");
return mongoTemplate.find(query, User.class, "users");
我在 MongoDB 中生成了 400.000 条记录。 在不使用上面写的排序行的情况下询问前 25 个用户时,我会在不到 50 毫秒内得到结果。
使用排序会持续超过 4 秒。
然后我为 FIRSTNAME、LASTNAME、EMAIL 创建了索引。单个索引,而不是组合索引
mongoTemplate.indexOps("users").ensureIndex(new Index("FIRSTNAME", Order.ASCENDING));
mongoTemplate.indexOps("users").ensureIndex(new Index("LASTNAME", Order.ASCENDING));
mongoTemplate.indexOps("users").ensureIndex(new Index("EMAIL", Order.ASCENDING));
创建这些索引后,查询再次持续超过 4 秒。
我的错误是什么?
-- 编辑 MongoDB 在控制台上写这个...
Thu Jul 04 10:10:11.442 [conn50] query mydb.users query: { query: { _id: { $exists: true } }, orderby: { LASTNAME: 1, FIRSTNAME: 1, EMAIL: 1 } } ntoreturn:25 ntoskip:0 nscanned:382424 scanAndOrder:1 keyUpdates:0 numYields: 2 locks(micros) r:6903475 nreturned:25 reslen:3669 4097ms
【问题讨论】:
标签: java spring mongodb spring-mvc