数据库架构:
{
"_id" : ObjectId("5178c458e4b0e2f3cee77d47"),
"userId" : NumberLong(1574631),
"type" : 2,
"text" : "a user connected to B",
"status" : 0,
"createdDate" : ISODate("2013-04-25T05:51:19.995Z"),
"modifiedDate" : ISODate("2013-04-25T05:51:19.995Z"),
"metadata" : "{\"INVITEE_NAME\":\"2344\",\"INVITEE\":1232143,\"INVITE_SENDER\":1574476,\"INVITE_SENDER_NAME\":\"123213\"}",
"opType" : 1,
"actorId" : NumberLong(1574630),
"actorName" : "2344"
}
DB stats :-
db.stats()
{
"db" : "UserNotificationDev2",
"collections" : 3,
"objects" : 78597973,
"avgObjSize" : 489.00035699393925,
"dataSize" : 38434436856,
"storageSize" : 41501835008,
"numExtents" : 42,
"indexes" : 2,
"indexSize" : 4272393328,
"fileSize" : 49301946368,
"nsSizeMB" : 16,
"dataFileVersion" : {
"major" : 4,
"minor" : 5
},
"ok" : 1
}
索引:用户ID和_ID
我们试图为一个用户选择最新的21个通知。
db.userNotification.find({ "userId" : 53 }).limit(21).sort({ "_id" : -1 });
但是这个查询花费的时间太长了。Fri Apr 26 05:39:55.563 [conn156] 查询 UserNotificationDev2.userNotification: { query: { userId: 53 }, orderby: { _id: -1 } } cursorid:225321382318166794 ntoreturn:21 ntoskip:0 nscanned:266025 keyUpdates:0 numYields: 2 locks(micros) r:4224498 nreturned:21 reslen:10295 耗时2581毫秒。
即使是计数也需要很长一段时间。
Fri Apr 26 05:47:46.005 [conn159] command UserNotificationDev2.$cmd command: { count: "userNotification", query: { userId: 53 } } ntoreturn:1 keyUpdates:0 numYields: 11 locks(micros) r:9753890 reslen:48 5022ms
我们的查询有什么问题吗?
请帮忙!!!
如果我们的模式不适合存储用户通知,请提供建议。我们尝试过将用户和通知组成嵌入式文档,但是文档大小限制了我们只能存储约50,000个通知。因此我们进行了更改。
explain()
吗,特别是db.userNotification.find({ "userId" : 53 })
? - cirrus