对不同集合的类似查询具有截然不同的性能

时间:2016-03-30 19:53:35

标签: mongodb mongodb-query

此查询需要66秒:

{
    "op" : "query",
    "ns" : "pipelines_odd.CachedUrl_319",
    "query" : {
        "find" : "CachedUrl_319",
        "filter" : {
            "$and" : [
                {
                    "updated_at" : {
                        "$lt" : ISODate("2016-03-23T19:26:36.207Z")
                    }
                },
                {
                    "host_pattern_id" : 1844
                }
            ]
        },
        "ntoreturn" : 100,
        "sort" : {
            "updated_at" : 1
        }
    },
    "keysExamined" : 0,
    "docsExamined" : 0,
    "hasSortStage" : true,
    "cursorExhausted" : true,
    "keyUpdates" : 0,
    "writeConflicts" : 0,
    "numYield" : 18150,
    "locks" : {
        "Global" : {
            "acquireCount" : {
                "r" : NumberLong(36302)
            },
            "acquireWaitCount" : {
                "r" : NumberLong(3078)
            },
            "timeAcquiringMicros" : {
                "r" : NumberLong(3054408)
            }
        },
        "Database" : {
            "acquireCount" : {
                "r" : NumberLong(18151)
            }
        },
        "Collection" : {
            "acquireCount" : {
                "r" : NumberLong(18151)
            }
        }
    },
    "nreturned" : 0,
    "responseLength" : 20,
    "millis" : 66576,
    "execStats" : {
        "stage" : "ENSURE_SORTED",
        "nReturned" : 0,
        "executionTimeMillisEstimate" : 0,
        "works" : 6,
        "advanced" : 0,
        "needTime" : 5,
        "needYield" : 0,
        "saveState" : 0,
        "restoreState" : 0,
        "isEOF" : 1,
        "invalidates" : 0,
        "nDropped" : 0,
        "inputStage" : {
            "stage" : "OR",
            "nReturned" : 0,
            "executionTimeMillisEstimate" : 0,
            "works" : 6,
            "advanced" : 0,
            "needTime" : 5,
            "needYield" : 0,
            "saveState" : 0,
            "restoreState" : 0,
            "isEOF" : 1,
            "invalidates" : 0,
            "dupsTested" : 0,
            "dupsDropped" : 0,
            "locsForgotten" : 0,
            "inputStages" : [
                {
                    "stage" : "SORT",
                    "nReturned" : 0,
                    "executionTimeMillisEstimate" : 0,
                    "works" : 3,
                    "advanced" : 0,
                    "needTime" : 2,
                    "needYield" : 0,
                    "saveState" : 0,
                    "restoreState" : 0,
                    "isEOF" : 1,
                    "invalidates" : 0,
                    "sortPattern" : {
                        "updated_at" : 1
                    },
                    "memUsage" : 0,
                    "memLimit" : 33554432,
                    "limitAmount" : 100,
                    "inputStage" : {
                        "stage" : "SORT_KEY_GENERATOR",
                        "nReturned" : 0,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 2,
                        "advanced" : 0,
                        "needTime" : 1,
                        "needYield" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "inputStage" : {
                            "stage" : "FETCH",
                            "filter" : {
                                "updated_at" : {
                                    "$lt" : ISODate("2016-03-23T19:26:36.207Z")
                                }
                            },
                            "nReturned" : 0,
                            "executionTimeMillisEstimate" : 0,
                            "works" : 1,
                            "advanced" : 0,
                            "needTime" : 0,
                            "needYield" : 0,
                            "saveState" : 0,
                            "restoreState" : 0,
                            "isEOF" : 1,
                            "invalidates" : 0,
                            "docsExamined" : 0,
                            "alreadyHasObj" : 0,
                            "inputStage" : {
                                "stage" : "IXSCAN",
                                "nReturned" : 0,
                                "executionTimeMillisEstimate" : 0,
                                "works" : 1,
                                "advanced" : 0,
                                "needTime" : 0,
                                "needYield" : 0,
                                "saveState" : 0,
                                "restoreState" : 0,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "keyPattern" : {
                                    "host_pattern_id" : 1
                                },
                                "indexName" : "host_pattern_id_1",
                                "isMultiKey" : false,
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 1,
                                "direction" : "forward",
                                "indexBounds" : {
                                    "host_pattern_id" : [
                                        "[1844, 1844]"
                                    ]
                                },
                                "keysExamined" : 0,
                                "dupsTested" : 0,
                                "dupsDropped" : 0,
                                "seenInvalidated" : 0
                            }
                        }
                    }
                },
                {
                    "stage" : "SORT",
                    "nReturned" : 0,
                    "executionTimeMillisEstimate" : 0,
                    "works" : 3,
                    "advanced" : 0,
                    "needTime" : 2,
                    "needYield" : 0,
                    "saveState" : 0,
                    "restoreState" : 0,
                    "isEOF" : 1,
                    "invalidates" : 0,
                    "sortPattern" : {
                        "updated_at" : 1
                    },
                    "memUsage" : 0,
                    "memLimit" : 33554432,
                    "inputStage" : {
                        "stage" : "SORT_KEY_GENERATOR",
                        "nReturned" : 0,
                        "executionTimeMillisEstimate" : 0,
                        "works" : 2,
                        "advanced" : 0,
                        "needTime" : 1,
                        "needYield" : 0,
                        "saveState" : 0,
                        "restoreState" : 0,
                        "isEOF" : 1,
                        "invalidates" : 0,
                        "inputStage" : {
                            "stage" : "FETCH",
                            "filter" : {
                                "updated_at" : {
                                    "$lt" : ISODate("2016-03-23T19:26:36.207Z")
                                }
                            },
                            "nReturned" : 0,
                            "executionTimeMillisEstimate" : 0,
                            "works" : 1,
                            "advanced" : 0,
                            "needTime" : 0,
                            "needYield" : 0,
                            "saveState" : 0,
                            "restoreState" : 0,
                            "isEOF" : 1,
                            "invalidates" : 0,
                            "docsExamined" : 0,
                            "alreadyHasObj" : 0,
                            "inputStage" : {
                                "stage" : "IXSCAN",
                                "nReturned" : 0,
                                "executionTimeMillisEstimate" : 0,
                                "works" : 1,
                                "advanced" : 0,
                                "needTime" : 0,
                                "needYield" : 0,
                                "saveState" : 0,
                                "restoreState" : 0,
                                "isEOF" : 1,
                                "invalidates" : 0,
                                "keyPattern" : {
                                    "host_pattern_id" : 1
                                },
                                "indexName" : "host_pattern_id_1",
                                "isMultiKey" : false,
                                "isUnique" : false,
                                "isSparse" : false,
                                "isPartial" : false,
                                "indexVersion" : 1,
                                "direction" : "forward",
                                "indexBounds" : {
                                    "host_pattern_id" : [
                                        "[1844, 1844]"
                                    ]
                                },
                                "keysExamined" : 0,
                                "dupsTested" : 0,
                                "dupsDropped" : 0,
                                "seenInvalidated" : 0
                            }
                        }
                    }
                }
            ]
        }
    },
    "ts" : ISODate("2016-03-30T19:27:56.347Z"),
    "allUsers" : [ ],
    "user" : ""
}

虽然针对大小相同的不同集合的类似查询在10毫秒内运行:

{
    "op" : "query",
    "ns" : "pipelines_odd.CachedUrl_257",
    "query" : {
        "find" : "CachedUrl_257",
        "filter" : {
            "$or" : [
                {
                    "$and" : [
                        {
                            "updated_at" : {
                                "$lt" : ISODate("2016-01-23T20:55:52.174Z")
                            }
                        },
                        {
                            "host_pattern_id" : 5046
                        }
                    ]
                }
            ]
        },
        "ntoreturn" : 100,
        "sort" : {
            "updated_at" : 1
        }
    },
    "cursorid" : NumberLong("16448827549027"),
    "keysExamined" : 100,
    "docsExamined" : 100,
    "keyUpdates" : 0,
    "writeConflicts" : 0,
    "numYield" : 1,
    "locks" : {
        "Global" : {
            "acquireCount" : {
                "r" : NumberLong(4)
            }
        },
        "Database" : {
            "acquireCount" : {
                "r" : NumberLong(2)
            }
        },
        "Collection" : {
            "acquireCount" : {
                "r" : NumberLong(2)
            }
        }
    },
    "nreturned" : 100,
    "responseLength" : 42183,
    "millis" : 10,
    "execStats" : {
        "stage" : "CACHED_PLAN",
        "nReturned" : 100,
        "executionTimeMillisEstimate" : 10,
        "works" : 100,
        "advanced" : 100,
        "needTime" : 0,
        "needYield" : 0,
        "saveState" : 2,
        "restoreState" : 1,
        "isEOF" : 0,
        "invalidates" : 0,
        "inputStage" : {
            "stage" : "FETCH",
            "filter" : {
                "host_pattern_id" : {
                    "$eq" : 5046
                }
            },
            "nReturned" : 100,
            "executionTimeMillisEstimate" : 10,
            "works" : 100,
            "advanced" : 100,
            "needTime" : 0,
            "needYield" : 0,
            "saveState" : 2,
            "restoreState" : 1,
            "isEOF" : 0,
            "invalidates" : 0,
            "docsExamined" : 100,
            "alreadyHasObj" : 0,
            "inputStage" : {
                "stage" : "IXSCAN",
                "nReturned" : 100,
                "executionTimeMillisEstimate" : 0,
                "works" : 100,
                "advanced" : 100,
                "needTime" : 0,
                "needYield" : 0,
                "saveState" : 2,
                "restoreState" : 1,
                "isEOF" : 0,
                "invalidates" : 0,
                "keyPattern" : {
                    "updated_at" : 1
                },
                "indexName" : "updated_at_1",
                "isMultiKey" : false,
                "isUnique" : false,
                "isSparse" : false,
                "isPartial" : false,
                "indexVersion" : 1,
                "direction" : "forward",
                "indexBounds" : {
                    "updated_at" : [
                        "(true, new Date(1453582552174))"
                    ]
                },
                "keysExamined" : 100,
                "dupsTested" : 0,
                "dupsDropped" : 0,
                "seenInvalidated" : 0
            }
        }
    },
    "ts" : ISODate("2016-03-23T20:55:40.203Z"),
    "allUsers" : [ ],
    "user" : ""
}

两个集合都有相似的大小和相同的索引:

PipelinesCluster1:SECONDARY> db.CachedUrl_319.count()
24383730
PipelinesCluster1:SECONDARY> db.CachedUrl_319.getIndexes()
[
    {
        "ns" : "pipelines_odd.CachedUrl_319",
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_"
    },
    {
        "ns" : "pipelines_odd.CachedUrl_319",
        "v" : 1,
        "key" : {
            "updated_at" : 1
        },
        "name" : "updated_at_1",
        "background" : true
    },
    {
        "ns" : "pipelines_odd.CachedUrl_319",
        "v" : 1,
        "key" : {
            "host_pattern_id" : 1
        },
        "name" : "host_pattern_id_1",
        "background" : true
    }
]
PipelinesCluster1:SECONDARY> db.CachedUrl_257.count()
24697281
PipelinesCluster1:SECONDARY> db.CachedUrl_257.getIndexes()
[
    {
        "ns" : "pipelines_odd.CachedUrl_257",
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_"
    },
    {
        "ns" : "pipelines_odd.CachedUrl_257",
        "v" : 1,
        "key" : {
            "updated_at" : 1
        },
        "name" : "updated_at_1",
        "background" : true
    },
    {
        "ns" : "pipelines_odd.CachedUrl_257",
        "v" : 1,
        "key" : {
            "host_pattern_id" : 1
        },
        "name" : "host_pattern_id_1",
        "background" : true
    }
]

我了解查询性能可能因数据库负载和其他因素而异,但针对CachedUrl_319的查询始终较慢,而针对CachedUrl_257的查询始终会立即返回。可能导致这种情况的原因有什么办法可以解决它吗?

1 个答案:

答案 0 :(得分:0)

我认为你是第二次查询缓存的。

您创建了一个索引,因此mongodb会自动为您处理缓存。 常见问题@ mongodb.org

如果你看一下你在execStats

中发布的第二个system.profile集合
    "execStats" : {
    "stage" : "CACHED_PLAN",  <-- you were cached

将它与第一个进行比较,您将看到第一个system.profile集合具有不同的阶段,例如

    "execStats" : {
    "stage" : "ENSURE_SORTED"

您还可以查看&#34; inputStage&#34; ,你会看到第二个刚刚取出。

        "inputStage" : {
        "stage" : "FETCH",

第一个做得更多,例如&#34; SORT&#34;和&#34; SORT_KEY_GENERATOR&#34;。