简体   繁体   English

排序时的Mongodb性能问题

[英]Mongodb performance issue when sorting

I'm trying to switch mysql to mongodb. 我正在尝试将mysql切换到mongodb。 But i have an issue about sorting performance. 但是我有一个关于排序性能的问题。

I have millions of documents as following; 我有数百万份文件,如下所示;

{
    "_id": ObjectId("5af7cbda7500fc509c3098ce"),
    "name": "Task Name",
    "category": "performance",
    "subIssues": [
        {
            "taskId": 10,
            "description": "Task description",
            "createdAt": "2018-05-11 14:37:07.000Z"
        },
        {
            "taskId": 11,
            "description": "Task description",
            "createdAt": "2018-05-11 14:37:07.000Z"
        },
        {
            "taskId": 12,
            "description": "Task description",
            "createdAt": "2018-05-11 14:37:07.000Z"
        }
    ]
}

I want to sorting by "subIssues.taskId", the query is ".tasks.find({"name": "performance"}).limit(10).sort({"subIssues.taskId": -1})". 我想按“ subIssues.taskId”进行排序,查询为“ .tasks.find({“ name”:“ performance”})。limit(10).sort({“ subIssues.taskId”:-1})“ 。 But this query works too slow. 但是此查询工作太慢。 I tried another field (sorting by "name"), that worked very fast but sub array wasn't. 我尝试了另一个字段(按“名称”排序),该字段运行非常快,但子数组却没有。 Collection indexes; 收集指标;

[
    {
        "_id" : 1
    },
    {
        "name" : 1.0
    },
    {
        "subIssues.taskId" : 1.0
    }
]

Explain Output; 解释输出;

{
    "queryPlanner" : {
        "plannerVersion" : 1,
        "namespace" : "testing.tasks",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "name" : {
                "$eq" : "performance"
            }
        },
        "winningPlan" : {
            "stage" : "SORT",
            "sortPattern" : {
                "subIssues.taskId" : -1.0
            },
            "limitAmount" : 10,
            "inputStage" : {
                "stage" : "SORT_KEY_GENERATOR",
                "inputStage" : {
                    "stage" : "FETCH",
                    "inputStage" : {
                        "stage" : "IXSCAN",
                        "keyPattern" : {
                            "name" : 1.0
                        },
                        "indexName" : "name_1",
                        "isMultiKey" : false,
                        "multiKeyPaths" : {
                            "name" : []
                        },
                        "isUnique" : false,
                        "isSparse" : false,
                        "isPartial" : false,
                        "indexVersion" : 2,
                        "direction" : "forward",
                        "indexBounds" : {
                            "name" : [ 
                                "[\"performance\", \"performance\"]"
                            ]
                        }
                    }
                }
            }
        },
        "rejectedPlans" : [ 
            {
                "stage" : "SORT",
                "sortPattern" : {
                    "subIssues.taskId" : -1.0
                },
                "limitAmount" : 10,
                "inputStage" : {
                    "stage" : "SORT_KEY_GENERATOR",
                    "inputStage" : {
                        "stage" : "FETCH",
                        "filter" : {
                            "name" : {
                                "$eq" : "performance"
                            }
                        },
                        "inputStage" : {
                            "stage" : "IXSCAN",
                            "keyPattern" : {
                                "subIssues.taskId" : 1.0
                            },
                            "indexName" : "subIssues.taskId_1",
                            "isMultiKey" : true,
                            "multiKeyPaths" : {
                                "subIssues.taskId" : [ 
                                    "subIssues"
                                ]
                            },
                            "isUnique" : false,
                            "isSparse" : false,
                            "isPartial" : false,
                            "indexVersion" : 2,
                            "direction" : "backward",
                            "indexBounds" : {
                                "subIssues.taskId" : [ 
                                    "[MaxKey, MinKey]"
                                ]
                            }
                        }
                    }
                }
            }
        ]
    },
    "serverInfo" : {
        "host" : "5113848ca8f8",
        "port" : 27017,
        "version" : "3.6.4"
    },
    "ok" : 1.0
}

I use mongo 3.6 on centos 7, 16 core, 32gb ram. 我在centos 7、16核心,32GB内存上使用mongo 3.6。

what are your suggestions? 您有什么建议?

Try indexing the field you want to sort: db.records.createIndex( { subIssues.taskId: 1 } ) 尝试索引要排序的字段:db.records.createIndex({subIssues.taskId:1})

But i don't think this will work, change your data structure :) 但我认为这不起作用,请更改您的数据结构:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM