Mongo db慢查询

时间:2016-08-10 22:47:54

标签: mongodb

我的查询如下:

db.Users.find({
    "Profile.Type" : {
        "$ne" : NumberInt(4)
    }, 
    "JoinDate" : {
        "$gte" : ISODate("2016-08-09T23:00:00.000+0100"), 
        "$lte" : ISODate("2016-08-10T23:00:00.000+0100")
    }, 
    "Category" : {
        "$ne" : null
    }})

运行需要大约8秒 - 这似乎很长一段时间!数据库中大约有200万行,此查询大约有10,000个。

我有一个索引(我认为应该有帮助)

{ 
    "Profile.Type" : 1.0, 
    "Category" : 1.0, 
    "JoinDate" : 1.0, 
    "Profile.ParentUserIds" : 1.0, 
    "Profile.AdminId" : 1.0
}

还有一个只是

{  
    "JoinDate" : 1.0
}

...我提到的是因为.explain()似乎暗示它使用的是第二个索引,而不是第一个索引。

有什么想法吗?

1 个答案:

答案 0 :(得分:0)

尝试使用hint()强制使用第一个索引并查看性能是否有所提高(应减少扫描文档的数量)

db.Users.find({
    "Profile.Type" : {
        "$ne" : NumberInt(4)
    }, 
    "JoinDate" : {
        "$gte" : ISODate("2016-08-09T23:00:00.000+0100"), 
        "$lte" : ISODate("2016-08-10T23:00:00.000+0100")
    }, 
    "Category" : {
        "$ne" : null
    }}).hint({ 
      "Profile.Type" : 1.0, 
      "Category" : 1.0, 
      "JoinDate" : 1.0, 
      "Profile.ParentUserIds" : 1.0, 
      "Profile.AdminId" : 1.0
    })
相关问题