【问题标题】:How can I perform this mongoDB lookup aggregation faster?如何更快地执行此 mongoDB 查找聚合?
【发布时间】:2019-11-18 04:17:26
【问题描述】:

我想在 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 更快的方法?

【问题讨论】:

  • 你能展示你的集合架构吗?这将帮助我们找出为什么要花这么多时间。
  • 请为该聚合发布 db.collection.explain().aggregate(...)。它将显示执行语句背后的 mongo 策略。
  • $lookup 在您的查询中使用了三次,这将对性能产生巨大影响。查找类似于在 RDBMS 世界中执行左外连接。我建议您使用非规范化数据重新设计架构。
  • @ClementAmarnath 你的意思是修改数据库吗?我只有读取权限!
  • 抱歉,请与正在修改访问权限的人一起尝试。在这种情况下,重新设计架构将大大提高性能。现有设计中的集合看起来像规范化的表格。

标签: mongodb performance nosql aggregation


【解决方案1】:

始终在 mongodb 中为您正在执行查找的引用表中的字段创建索引,这将减少查询的执行时间。

【讨论】:

  • 为包含 15 个字段的 7 亿个条目的集合创建一个新索引需要多长时间?
猜你喜欢
  • 1970-01-01
  • 2021-01-08
  • 1970-01-01
  • 1970-01-01
  • 2019-06-25
  • 2021-11-14
  • 1970-01-01
  • 1970-01-01
  • 2017-11-08
相关资源
最近更新 更多