加快构面聚合查询-MongoDB

时间:2018-08-13 13:21:11

标签: mongodb aggregation-framework facet

文档格式:

{
    ...,
    tag: [
      { 
        name: 'weather',
        value: 'hot'
      },
      {
        name: 'special',
        value: 'transparent'
      }
    ],
    type: 'mansion'
}

Facet查询:

db.getCollection('houses').aggregate([
  { 
    '$facet':
        { tags: [
            { '$unwind': '$tag' },
            { '$sortByCount': '$tag' },
            { '$limit': 20 } 
          ],
          types: [ { '$sortByCount': '$type' } ],
          total: [ { '$count': 'count' } ]
        }
  } 
])

说明:

{
    "stages" : [ 
        {
            "$cursor" : {
                "query" : {},
                "fields" : {
                    "tag" : 1,
                    "type" : 1,
                    "_id" : 0
                },
                "queryPlanner" : {
                    "plannerVersion" : 1,
                    "namespace" : "db.houses",
                    "indexFilterSet" : false,
                    "parsedQuery" : {},
                    "winningPlan" : {
                        "stage" : "COLLSCAN",
                        "direction" : "forward"
                    },
                    "rejectedPlans" : []
                }
            }
        }, 
        {
            "$facet" : {
                "tags" : [ 
                    {
                        "$unwind" : {
                            "path" : "$tag"
                        }
                    }, 
                    {
                        "$group" : {
                            "_id" : "$tag",
                            "count" : {
                                "$sum" : {
                                    "$const" : 1
                                }
                            }
                        }
                    }, 
                    {
                        "$sort" : {
                            "sortKey" : {
                                "count" : -1
                            },
                            "limit" : NumberLong(20)
                        }
                    }
                ],
                "types" : [ 
                    {
                        "$group" : {
                            "_id" : "$type",
                            "count" : {
                                "$sum" : {
                                    "$const" : 1
                                }
                            }
                        }
                    }, 
                    {
                        "$sort" : {
                            "sortKey" : {
                                "count" : -1
                            }
                        }
                    }
                ],
                "total" : [ 
                    {
                        "$group" : {
                            "_id" : {
                                "$const" : null
                            },
                            "count" : {
                                "$sum" : {
                                    "$const" : 1
                                }
                            }
                        }
                    }, 
                    {
                        "$project" : {
                            "_id" : false,
                            "count" : true
                        }
                    }
                ]
            }
        }
    ],
    "ok" : 1.0
}

我有一个收藏集houses,其中包含一百万个文档。我想获得各种标签和类型的计数。但是查询一百万个文档大约要花25秒。太慢了。

我为标记和类型创建了许多索引,但不适用于此查询。如何进行更快的方面查询?

0 个答案:

没有答案