我有2个mongo集合:
公司:每条记录都是一个拥有多个字段(城市,国家/地区等)的公司 - > 100k rows
{company_id:1, country:"USA", city:"New York",...}
{company_id:2, country:"Spain", city:"Valencia",... }
{company_id:3, country:"France", city:"Paris",... }
得分:有一些日期块,每个块都有一个company_id +得分,例如 - > 100k rows in each block
{date: 2016-05-29, company_id:1, score:90}
{date: 2016-05-29, company_id:2, score:87}
{date: 2016-05-29, company_id:3, score:75}
...
{date: 2016-05-22, company_id:1, score:88}
{date: 2016-05-22, company_id:2, score:87}
{date: 2016-05-22, company_id:3, score:76}
...
{date: 2016-05-15, company_id:1, score:91}
{date: 2016-05-15, company_id:2, score:82}
{date: 2016-05-15, company_id:3, score:73}
...
目标:
我想检索一些公司列表,这些公司可以按某些字段(国家/地区,城市,...)过滤+最新评分(2016-05-29),ordered by score descending
即:在一个集合中过滤,在另一个集合中过滤+订单
注意:scores.date
上有一个索引,我们可以轻松定位/预先计算最快的日期(本例中为2016-05-29)
尝试:
我一直在使用aggregate
尝试$lookup
查询。当过滤器完成(并且公司数量很少)时,查询会更快。
查询如下: -
db.companies.aggregate([
{$match: {"status": "running", "country": "USA", "city": "San Francisco",
"categories": { $in: ["Software"]}, dummy: false}},
{$lookup: {from: "scores", localField: "company_id", foreignField: "company_id", as:"scores"}},
{$unwind: "$scores"},
{$project: {_id: "$_id",
"company_id": "$company_id",
"company_name": "$company_name",
"status": "$status",
"city": "$city",
"country": "$country",
"categories": "$categories",
"dummy": "$dummy",
"score": "$scores.score",
"date": "$scores.date"}},
{$match: {"date" : ISODate("2016-05-29T00:00:00Z")}},
{$sort: {"score":-1}}
],{allowDiskUse: true})
但是当过滤器很小或空(更多公司)时,$sort
部分需要几秒钟。
db.companies.aggregate([
{$match: {"status": "running"}},
{$lookup: {from: "scores", localField: "company_id", foreignField: "company_id", as:"scores"}},
{$unwind: "$scores"},
{$project: {_id: "$_id",
"company_id": "$company_id",
"company_name": "$company_name",
"status": "$status",
"city": "$city",
"country": "$country",
"categories": "$categories",
"dummy": "$dummy",
"score": "$scores.score",
"date": "$scores.date"}},
{$match: {"date" : ISODate("2016-05-29T00:00:00Z")}},
{$sort: {"score":-1}}
],{allowDiskUse: true})
可能是因为过滤器找到的公司数量。 59行比89k更容易订购
> db.companies.count({"status": "running", "country": "USA", "city": "San Francisco", "categories": { $in: ["Software"]}, dummy: false})
59
> db.companies.count({"status": "running"})
89043
我尝试过不同的方法,按分数汇总,按日期过滤,按分数排序(索引日期+分数在这里非常有用),一切都很快,直到最后$match
当我过滤公司属性时
db.scores.aggregate([
{$match:{"date" : ISODate("2016-05-29T00:00:00Z")}},
{$sort:{"score":-1}},
{$lookup:{from: "companies", localField: "company_id", foreignField: "company_id", as:"companies"}},
{$unwind:"$companies"},
{$project: {_id: "$companies._id",
"company_id": "$companies.company_id",
"company_name": "$companies.company_name",
"status": "$companies.status",
"city": "$companies.city",
"country": "$companies.country",
"categories": "$companies.categories",
"dummy": "$companies.dummy"}},
"score": "$score",
"date": "$date"
{$match:{"status": "running", "country":"USA", "city": "San Francisco",
"categories": { $in: ["Software"]}, dummy: false}}
],{allowDiskUse: true})
使用这种方法,大过滤器(前面的例子)非常慢,而小过滤器(只是{"status": "running"}
)更快
任何方式加入两个集合,过滤两个集合并按一个字段排序?
答案 0 :(得分:2)
正如我所看到的,每个公司(不是很多)在不同日期只有几个分数。所以这是一种 1:很少的关系。
首先,我想到的是:为什么不把分数放在公司数据库中?
{ company_id:1,
country:"USA",
city:"New York",
...
scores: [
{date: 2016-05-29, score:90},
...
]
}
这样,结构与您的访问模式更加一致,您可以完全跳过查找部分。意思是,您可以定义正确的索引并使用find()
而不是聚合。
除此之外,我想知道为什么你使用allowDiskUse:true
标志,100k文件听起来不那么多,它们应该完全适合内存,甚至进入有限的(128M)聚合管道缓冲区。
要解释一下,为什么过滤器(短=非常有选择性,长=非常有选择性)表现不同,这取决于你开始的收集(得分与公司)
那么你应该看看: