MongoDB复合指数
我的收藏需要复合索引,但我不确定键的顺序 我的项目:MongoDB复合指数,mongodb,indexing,Mongodb,Indexing,我的收藏需要复合索引,但我不确定键的顺序 我的项目: { _id, location: { type: "Point", coordinates: [<lng>, <lat>] }, isActive: true, till: ISODate("2016-12-29T22:00:00.000Z"), createdAt : ISODate("2016-10-31T1
{
_id,
location: {
type: "Point",
coordinates: [<lng>, <lat>]
},
isActive: true,
till: ISODate("2016-12-29T22:00:00.000Z"),
createdAt : ISODate("2016-10-31T12:02:51.072Z"),
...
}
在human中,我需要地图可见部分上所有未过期、新添加的活动项-首先
创建此索引是否正常:
db.collection.createIndex( { "isActive": 1, "till": -1, "location": "2dsphere", "createdAt": -1 } )
对于磁盘使用率而言,性能的最佳顺序是什么?或者我必须创建几个索引
谢谢大家! 索引中字段的顺序应为:
db.collection.createIndex( { "isActive": 1, "createdAt": -1, "till": -1, "location": "2dsphere" } )
但是,布尔字段上的索引通常不是很有用,因为MongoDB平均仍需要访问一半的文档。因此,我建议您执行以下操作:
{“isActive”:1,“createdAt”:-1,“till”:-1,“location”:“2dsphere”}
)exp.find({'youquery'})
它将返回描述获胜计划执行情况的统计信息{“createdAt”:-1,“直到”:-1,“位置”:“2dsphere”}
),执行exp.find({'you query'})
将结果与前一个结果进行比较在Mongo中,许多事情依赖于数据及其访问模式。在您的集合中创建索引时要考虑的事情很少——
var ex = db.<collection>.explain("executionStats")
对集合应用两个索引,并执行ex.find().sort(),其中ex是可解释的游标。然后,您需要分析两种输出并进行比较,以确定最佳结果。谢谢您,Oleksandr我必须创建一些测试。。。但是有一些评论:isActive in~95%是正确的,但是出于一些统计原因,我做了一些额外的查询:
db.collection.find({$and:[{isActive:true},{'till':{$gte:new Date()}]})
和db.collection.find({$and:[{isActive true},{'till':{$gte:new Date()},{createdAt:{$gte:todate}}}})
,,没有排序和地理位置。kyes点的菜一样吗?另外,非常感谢来自敖德萨的基辅(-:)很好。对于“createdAt”字段用作过滤器的情况,索引顺序相同。但是,如果查询中没有“createdAt”字段(无论是在过滤器中还是在排序中),则不会使用建议的索引,并且可能最好只为“till”字段创建一个索引。但请记住,附加索引会降低写入和删除操作的性能
var ex = db.<collection>.explain("executionStats")
ex.find(<Your query>).sort(<sort predicate>)
1. {"location": "2dsphere" , "createdAt": -1}
2. {"till":1, "location": "2dsphere" , "createdAt": -1}