如何更快地执行此mongoDB查找聚合?

时间:2019-07-08 15:47:50

标签: mongodb performance nosql aggregation

我想在Robo 3T上运行以下mongoDB查询,但要取得结果永远是永远的:

db.getCollection('check').aggregate([
{ 
    $match:
    {
        $and:
        [
            {datetime: { "$gt" : new ISODate("2019-07-01 01:00:10.000Z")}},
        ]
    }
},
{
    $lookup:
        {
            from: "workstation",
            localField: "deviceid",
            foreignField: "_id",
            as: "workstation"
        }    
},
{
    $unwind: {path: "$workstation", preserveNullAndEmptyArrays: true}
},    
{
    $lookup:
        {
            from: "server",
            localField: "deviceid",
            foreignField: "_id",
            as: "server"
        }    
},
{
    $unwind: {path: "$server", preserveNullAndEmptyArrays: true}
},  

{
     $lookup: 
        {
            from: "site",
            let: {
                ssiteid : "$server.siteid",
                wsiteid : "$workstation.siteid"
            },
            pipeline: [
                { $match:
                    { $expr: {                            
                        $or: [
                            {$eq : ["$_id","$$ssiteid"]},
                            {$eq : ["$_id","$$wsiteid"]}
                        ]
                        }                                                
                    }                                               
                }],                
            as: "site"
        }      
},
{
    $unwind: {path: "$site", preserveNullAndEmptyArrays: true}
},  

{
     $lookup: 
        {
            from: "client",
            localField: "site.clientid",
            foreignField: "_id",
            as: "client"
        }      
},
{
    $unwind: {path: "$client", preserveNullAndEmptyArrays: true}
},

{ $project: { 
    "_id": 1, 
    "description": 1, 
    "extra": 1,
    "datetime": 1,
    "cname" : "$client.name",    
    "apiKey" : "$client.apiKey",
    "workstation": 1 ,
    "server":1
    }    
},

{ $match: 
    { "client.apiKey":"ae0a4c75230afae756fcfecd3d2838cf"}
},

 {$limit: 30}
])

但是,如果我删除了最后一场比赛,那么要花2秒才能得出结果!

作为集合的内容,以下是支票集合:

{
"_id" : ObjectId("5c1bbcfbfe78c90007af2676"),
"_class" : "dsadsa.ewrwer.werew,
"deviceid" : 943955,
"checkid" : "23303140",
"description" : "fdskfhsdj kfsdjfhskdjf hksdjfhsd kjfs",
"checkstatus" : "testerror",
"datetime" : ISODate("2018-12-04T15:55:00.000Z"),
"smsalerts" : 0,
"emailrecoveryalerts" : 1,
}    

和网站集:

{
"_id" : 126581,
"_class" : "dsadsa.ewrwer.werew,
"clientid" : 94011,
"name" : "dsadas, dsadsa",
"connectionOk" : 1,
"primaryRouter" : "",
"secondaryRouter" : "",
"lastUpdate" : ISODate("2018-01-02T13:00:04.713Z"),
"enabled" : false
}

和客户:

{
"_id" : 96763,
"_class" : "dsadsa.ewrwer.werew,
"name" : "JOhn Smith",
"viewDashboard" : 0,
"viewWkstsnAssets" : 0,
"dashboardUsername" : "none",
"timezone" : "",
"creationDate" : ISODate("2017-02-09T23:00:00.000Z"),
"serverCount" : 0,
"workstationCount" : 0,
"mobileDeviceCount" : 0,
"deviceCount" : 0,
"apiKey" : "dsaawedsa",
"lastUpdate" : ISODate("1970-03-17T11:00:00.000Z"),
"enabled" : false

}

有什么方法可以更快地执行类似(或类似的)查询吗?

此外,有没有比基于GUI的Robo mongo更快的访问mongoDB的方法?

1 个答案:

答案 0 :(得分:0)

总是在mongodb中为要为其执行查找的引用表中的字段创建索引,这将减少查询的执行时间。