MongoDB tips: using explainable object

This is just to quickly document an alternative way to run explain plan

assuming this is the query we want to run explain plan

2019-10-12T06:10:44.366-0400 I COMMAND [conn25251540] command device-event-store.events command: find { find: "events", filter: { $and: [ { aggregateId: { identifier: "990003494014489" } }, { version: { $gte: 0 } } ] }, sort: { version: 1 }, $db: "device-event-store", $clusterTime: { clusterTime: Timestamp(1570875044, 17), signature: { hash: BinData(0, 502D14059FA04824080CA91DAB899D434CFA3E87), keyId: 6727232173650214920 } }, lsid: { id: UUID("91bed447-28f5-4211-b3f8-58821d04060d") } } planSummary: COLLSCAN keysExamined:0 docsExamined:56299 hasSortStage:1 cursorExhausted:1 numYields:441 nreturned:78 reslen:44800 locks:{ Global: { acquireCount: { r: 884 } }, Database: { acquireCount: { r: 442 } }, Collection: { acquireCount: { r: 442 } } } protocol:op_msg 232ms

first let's create an explainable object with executionStats

In mongo shell
> var expobj = db.getSiblingDB("device-event-store").events.explain("executionStats")

> expobj.find({ $and: [ { aggregateId: { identifier: "990003494014489" } }, { version: { $gte: 0 } } ] }).sort({ version: 1 })

this will indeed return you the executionStats the same way. This works for other operation in the form of

> expobj.find()
> expobj.update()
> expobj.remove()
> expobj.aggregate()


{
        "queryPlanner" : {
                "plannerVersion" : 1,
                "namespace" : "device-event-store.events",
                "indexFilterSet" : false,
                "parsedQuery" : {
                        "$and" : [
                                {
                                        "aggregateId" : {
                                                "$eq" : {
                                                        "identifier" : "990003494014489"
                                                }
                                        }
                                },
                                {
                                        "version" : {
                                                "$gte" : 0
                                        }
                                }
                        ]
                },
                "winningPlan" : {
                        "stage" : "SORT",
                        "sortPattern" : {
                                "version" : 1
                        },
                        "inputStage" : {
                                "stage" : "SORT_KEY_GENERATOR",
                                "inputStage" : {
                                        "stage" : "COLLSCAN",
                                        "filter" : {
                                                "$and" : [
                                                        {
                                                                "aggregateId" : {
                                                                        "$eq" : {
                                                                                "identifier" : "990003494014489"
                                                                        }
                                                                }
                                                        },
                                                        {
                                                                "version" : {
                                                                        "$gte" : 0
                                                                }
                                                        }
                                                ]
                                        },
                                        "direction" : "forward"
                                }
                        }
                },
                "rejectedPlans" : [ ]
        },
        "executionStats" : {
                "executionSuccess" : true,
                "nReturned" : 56,
                "executionTimeMillis" : 238,
                "totalKeysExamined" : 0,
                "totalDocsExamined" : 49541,
                "executionStages" : {
                        "stage" : "SORT",
                        "nReturned" : 56,
                        "executionTimeMillisEstimate" : 214,
                        "works" : 49601,
                        "advanced" : 56,
                        "needTime" : 49544,
                        "needYield" : 0,
                        "saveState" : 388,
                        "restoreState" : 388,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "sortPattern" : {
                                "version" : 1
                        },
                        "memUsage" : 32248,
                        "memLimit" : 33554432,
                        "inputStage" : {
                                "stage" : "SORT_KEY_GENERATOR",
                                "nReturned" : 56,
                                "executionTimeMillisEstimate" : 214,
                                "works" : 49544,
                                "advanced" : 56,
                                "needTime" : 49487,
                                "needYield" : 0,
                                "saveState" : 388,
                                "restoreState" : 388,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "inputStage" : {
                                        "stage" : "COLLSCAN",
                                        "filter" : {
                                                "$and" : [
                                                        {
                                                                "aggregateId" : {
                                                                        "$eq" : {
                                                                                "identifier" : "990003494014489"
                                                                        }
                                                                }
                                                        },
                                                        {
                                                                "version" : {
                                                                        "$gte" : 0
                                                                }
                                                        }
                                                ]
                                        },
                                        "nReturned" : 56,
                                        "executionTimeMillisEstimate" : 214,
                                        "works" : 49543,
                                        "advanced" : 56,
                                        "needTime" : 49486,
                                        "needYield" : 0,
                                        "saveState" : 388,
                                        "restoreState" : 388,
                                        "isEOF" : 1,
                                        "invalidates" : 0,
                                        "direction" : "forward",
                                        "docsExamined" : 49541
                                }
                        }
                }
        },
        "serverInfo" : {
                "host" : "mongodb-mongo-1",
                "port" : 27017,
                "version" : "3.6.6",
                "gitVersion" : "6405d65b1d6432e138b44c13085d0c2fe235d6bd"
        },
        "ok" : 1,
        "operationTime" : Timestamp(1571424192, 1),
        "$clusterTime" : {
                "clusterTime" : Timestamp(1571424192, 1),
                "signature" : {
                        "hash" : BinData(0,"0iFcAvu7cX/YX0Tnri1x7LAhjVM="),
                        "keyId" : NumberLong("6718929765643845634")
                }
        }
}

Comments

Popular posts from this blog

MongoDB tip: 4 ways to modify replica set configuration

MongoDB Quick Note: BI Connector Issue

MongoDB Tips: Kill long running processes