我有Nav Collection,有178097条记录。
下面是样本集合。
{
"_id" : ObjectId("5911cdbe891123031552713e"),
"_class" : "org.maruti.mfm.data.entity.Nav",
"asAtDate" : ISODate("2017-05-07T16:00:00.000+0000"),
"nav" : 10.1102,
"repurchasePrice" : 0.0,
"salePrice" : 0.0,
"latest" : false,
"scheme" : DBRef("scheme", ObjectId("58d760a32ac5bf0405b69afa"))
}
从这些178097中,只有14174条记录最新:真实。
下面是我在java中的映射
@Document
公共类Nav扩展了BaseEntity {
private static final long serialVersionUID = 1L;
@Id
private String id;
@Indexed
@DBRef
private Scheme scheme;
@Indexed
private Date asAtDate;
private double nav;
private double repurchasePrice;
private double salePrice;
@Indexed
private boolean latest;
然而,当我使用以下方法查找具有最新标志的所有导航集合时。它花了超过1分钟才能找到178k记录中的大约14,000条记录。
public interface NavRepository extends MongoRepository<Nav, String> {
List<Nav> findByLatest(boolean latest);
}
无论如何都要改善表现。
解释结果
db.nav.find({latest:true}).explain();
{
"queryPlanner" : {
"plannerVersion" : 1,
"namespace" : "mfmunshi.nav",
"indexFilterSet" : false,
"parsedQuery" : {
"latest" : {
"$eq" : true
}
},
"winningPlan" : {
"stage" : "FETCH",
"inputStage" : {
"stage" : "IXSCAN",
"keyPattern" : {
"latest" : 1
},
"indexName" : "latest",
"isMultiKey" : false,
"multiKeyPaths" : {
"latest" : [ ]
},
"isUnique" : false,
"isSparse" : false,
"isPartial" : false,
"indexVersion" : 2,
"direction" : "forward",
"indexBounds" : {
"latest" : [
"[true, true]"
]
}
}
},
"rejectedPlans" : [ ]
},
"serverInfo" : {
"host" : "Jigars-MacBook-Pro.local",
"port" : 27017,
"version" : "3.4.2",
"gitVersion" : "3f76e40c105fc223b3e5aac3e20dcd026b83b38b"
},
"ok" : 1
}
当我从终端窗口触发时,查询花了0.057秒。