在MongoDB查询中优化$group
我想用$group优化MongoDB查询聚合,$match部分速度非常快,但当我尝试按坐标分组数据并推入数组2字段时,请求速度非常慢。但是有可能优化$group部分吗?我已经在用allowDiskUse了 详情: MongoDB版本:4.0.10 RAM:32GO 执行时间:25秒 文件数目:10 826 222 以下是查询:在MongoDB查询中优化$group,mongodb,performance,query-performance,Mongodb,Performance,Query Performance,我想用$group优化MongoDB查询聚合,$match部分速度非常快,但当我尝试按坐标分组数据并推入数组2字段时,请求速度非常慢。但是有可能优化$group部分吗?我已经在用allowDiskUse了 详情: MongoDB版本:4.0.10 RAM:32GO 执行时间:25秒 文件数目:10 826 222 以下是查询: [ { "$match": { "$or": [
[
{
"$match": {
"$or": [
{
"$and": [
{
"$or": [
{
"aliments.fruits": "banana"
},
{
"parents": {
"$in": [
"banana"
]
}
}
]
}
]
},
{
"aliments": {
"$in": [
"banana"
]
}
}
]
}
},
{
"$group": {
"_id": {
"coordinates": "$coordinates"
},
"File": {
"$push": {
"Id": "$_id",
"Paths": "$path"
}
}
}
}
],{allowDiskUse : true}
数据示例:
{
"_id" : ObjectId("5f7ed5907e170000b2005fa5"),
"path" : "C:\\photos\\test_file_20.docx",
"text" : "banana is good",
"type" : "text",
"page" : 1,
"aliments" : {
"fruits" : "banana",
"vegetable" : "bean"
},
"parents" : [
"banana",
"coco",
"orange"
],
"coordinates" : {
"type" : "Point",
"coordinates" : [
48.8534,
2.3488
]
},
"date" : ISODate("2020-10-08T09:02:08.700Z"),
"name" : "test_file_20",
}
数据库统计:
{
"db" : "505cef81-5d89-44f6-80ad-4721e93d9715",
"collections" : 4,
"views" : 0,
"objects" : 14,
"avgObjSize" : 5568.64285714286,
"dataSize" : 77961.0,
"storageSize" : 118784.0,
"numExtents" : 0,
"indexes" : 4,
"indexSize" : 69632.0,
"fsUsedSize" : 228811964416.0,
"fsTotalSize" : 254930128896.0,
"ok" : 1.0
}
我试过这个
在我创建的数据集上,速度似乎快了一点。但我只添加了10000个元素,并且我制作了一些随机数据,所以它不能代表您的数据库
[
{
"$match": {
"$or": [
{
"$or": [
{
"aliments.fruits": "banana"
},
{
"parents": {
"$in": [
"banana"
]
}
}
]
},
{
"aliments": {
"$in": [
"banana"
]
}
}
]
}
},
{
"$group": {
"_id": "$coordinates.coordinates",
"ids": {
"$push": "$_id"
},
"files": {
"$push": "$path"
}
}
}
]
你能提供你的数据样本吗?当然!我做到了