mongo java 创建索引,Mongo和Java:为聚合框架创建索引

Situation: I have collection with huge amount of documents after map reduce(aggregation). Documents in the collection looks like this:

/* 0 */

{

"_id" : {

"appId" : ObjectId("1"),

"timestamp" : ISODate("2014-04-12T00:00:00.000Z"),

"name" : "GameApp",

"user" : "test@mail.com",

"type" : "game"

},

"value" : {

"count" : 2

}

}

/* 1 */

{

"_id" : {

"appId" : ObjectId("2"),

"timestamp" : ISODate("2014-04-29T00:00:00.000Z"),

"name" : "ScannerApp",

"user" : "newUser@company.com",

"type" : "game"

},

"value" : {

"count" : 5

}

}

...

And I searching inside this collection with aggregation framework:

db.myCollection.aggregate([match, project, group, sort, skip, limit]); // aggregation can return result on Daily or Monthly time base depends of user search criteria, with pagination etc...

Possible search criteria:

1. {appId, timestamp, name, user, type}

2. {appId, timestamp}

3. {name, user}

I'm getting correct result, exactly what I need. But from optimisation point of view I have doubts about indexing.

Questions:

Is it possible to create indexes for such collection?

How I can create indexes for such object with complex _id field?

How I can do analog of db.collection.find().explain() to verify which index used?

And is good idea to index such collection or its my performance paranoia?

Answer summarisation:

MongoDB creates index by _id field automatically but that is useless in a case of complex _id field like in an example. For field like: _id: {name: "", timestamp: ""} you must use index like that: *.ensureIndex({"_id.name": 1, "_id.timestamp": 1}) only after that your collection will be indexed in proper way by _id field.

For tracking how your indexes works with Mongo Aggregation you can not use db.myCollection.aggregate().explain() and proper way of doing that is:

db.runCommand({

aggregate: "collection_name",

pipeline: [match, proj, group, sort, skip, limit],

explain: true

})

My testing on local computer sows that such indexing seems to be good idea. But this is require more testing with big collections.

解决方案

First, indexes 1 and 3 are probably worth investigating. As for explain, you can pass explain as an option to your pipeline. You can find docs here and an example here

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值