简体   繁体   中英

Mongodb performance issue when sorting

I'm trying to switch mysql to 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})". 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.

what are your suggestions?

Try indexing the field you want to sort: db.records.createIndex( { subIssues.taskId: 1 } )

But i don't think this will work, change your data structure :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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