I
I
Ilya Karavaev2017-01-25 08:09:38
MongoDB
Ilya Karavaev, 2017-01-25 08:09:38

How to speed up count query in mongodb?

Please tell me what can be done about this problem.
There is a composite index for three fields TIN, USRN and STATUS. There are more than a million records in the database with the same index data. A selection of 50 records is instantaneous, but pagination requires counting the number of records, counting the number of records in the cursor takes so long that it falls off by a timeout of 30 seconds.
Why doesn't monga count records by index? And how can I make her do it?
Maybe there is some Jedi trick of counting that bypasses the count function?
Explain query:

{
    "queryPlanner" : {
        "plannerVersion" : NumberInt(1),
        "namespace" : "db.history",
        "indexFilterSet" : false,
        "parsedQuery" : {
            "$and" : [
                {
                    "source.egrul" : {
                        "$eq" : "1111111111111"
                    }
                },
                {
                    "source.inn" : {
                        "$eq" : "6111111111"
                    }
                },
                {
                    "$not" : {
                        "status" : {
                            "$eq" : NumberInt(-100)
                        }
                    }
                }
            ]
        },
        "winningPlan" : {
            "stage" : "FETCH",
            "filter" : {
                "$and" : [
                    {
                        "$not" : {
                            "status" : {
                                "$eq" : NumberInt(-100)
                            }
                        }
                    },
                    {
                        "source.inn" : {
                            "$eq" : "6111111111"
                        }
                    }
                ]
            },
            "inputStage" : {
                "stage" : "IXSCAN",
                "keyPattern" : {
                    "source.egrul" : NumberInt(1),
                    "source.inn" : NumberInt(1),
                    "status" : NumberInt(1)
                },
                "indexName" : "request_list",
                "isMultiKey" : true,
                "direction" : "forward",
                "indexBounds" : {
                    "source.egrul" : [
                        "[\"1111111111111\", \"1111111111111\"]"
                    ],
                    "source.inn" : [
                        "[MinKey, MaxKey]"
                    ],
                    "status" : [
                        "[MinKey, -100)",
                        "(-100, MaxKey]"
                    ]
                }
            }
        },
        "rejectedPlans" : [

        ]
    },
    "executionStats" : {
        "executionSuccess" : true,
        "nReturned" : NumberInt(578422),
        "executionTimeMillis" : NumberInt(3184),
        "totalKeysExamined" : NumberInt(578422),
        "totalDocsExamined" : NumberInt(578422),
        "executionStages" : {
            "stage" : "FETCH",
            "filter" : {
                "$and" : [
                    {
                        "$not" : {
                            "status" : {
                                "$eq" : NumberInt(-100)
                            }
                        }
                    },
                    {
                        "source.inn" : {
                            "$eq" : "6111111111"
                        }
                    }
                ]
            },
            "nReturned" : NumberInt(578422),
            "executionTimeMillisEstimate" : NumberInt(2930),
            "works" : NumberInt(578423),
            "advanced" : NumberInt(578422),
            "needTime" : NumberInt(0),
            "needFetch" : NumberInt(0),
            "saveState" : NumberInt(4519),
            "restoreState" : NumberInt(4519),
            "isEOF" : NumberInt(1),
            "invalidates" : NumberInt(0),
            "docsExamined" : NumberInt(578422),
            "alreadyHasObj" : NumberInt(0),
            "inputStage" : {
                "stage" : "IXSCAN",
                "nReturned" : NumberInt(578422),
                "executionTimeMillisEstimate" : NumberInt(800),
                "works" : NumberInt(578423),
                "advanced" : NumberInt(578422),
                "needTime" : NumberInt(0),
                "needFetch" : NumberInt(0),
                "saveState" : NumberInt(4519),
                "restoreState" : NumberInt(4519),
                "isEOF" : NumberInt(1),
                "invalidates" : NumberInt(0),
                "keyPattern" : {
                    "source.egrul" : NumberInt(1),
                    "source.inn" : NumberInt(1),
                    "status" : NumberInt(1)
                },
                "indexName" : "request_list",
                "isMultiKey" : true,
                "direction" : "forward",
                "indexBounds" : {
                    "source.egrul" : [
                        "[\"1111111111111\", \"1111111111111\"]"
                    ],
                    "source.inn" : [
                        "[MinKey, MaxKey]"
                    ],
                    "status" : [
                        "[MinKey, -100)",
                        "(-100, MaxKey]"
                    ]
                },
                "keysExamined" : NumberInt(578422),
                "dupsTested" : NumberInt(578422),
                "dupsDropped" : NumberInt(0),
                "seenInvalidated" : NumberInt(0),
                "matchTested" : NumberInt(0)
            }
        },
        "allPlansExecution" : [

        ]
    },
    "serverInfo" : {
        "host" : "db.local",
        "port" : NumberInt(27017),
        "version" : "3.0.4",
        "gitVersion" : "0481c958daeb2969800511e7475dc66986fa9ed5"
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
lega, 2017-01-25
@lega

The presence of an index does not guarantee its use, profile the query, most likely the index does not work (for example, if you use a regex filter).

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question