Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/mongodb/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
向MongoDB查询添加筛选器会增加count()所需的时间_Mongodb_Mongodb Query - Fatal编程技术网

向MongoDB查询添加筛选器会增加count()所需的时间

向MongoDB查询添加筛选器会增加count()所需的时间,mongodb,mongodb-query,Mongodb,Mongodb Query,我有一个名为listings的集合,其中包含超过500万个文档和索引(仅相关),其中包含键: {u id:1} {“日期”:1} {“原因列表类型”:1} 我正在尝试运行查询 db.listings.count({ dated: { $in: [ISODate("2018-02-17T00:00:00Z"), ISODate("2018-02-16T00:00:00Z"),ISODate("2018-02-10T00:00:00Z")] }, causel

我有一个名为
listings
的集合,其中包含超过500万个文档和索引(仅相关),其中包含键:
{u id:1}

{“日期”:1}

{“原因列表类型”:1}

我正在尝试运行查询

db.listings.count({
    dated: {
        $in: [ISODate("2018-02-17T00:00:00Z"), ISODate("2018-02-16T00:00:00Z"),ISODate("2018-02-10T00:00:00Z")]
    },
    causelist_type: {$ne: -1}
})
需要130秒。(通过在命令前面加上
print(new Date())
记录)

但是,如果我删除过滤器
causelist_type:{$ne:-1}
,查询几乎会立即运行(~1s)

explain().queryPlanner
对于带有
causelist\u type
子句的查询:

{
    "plannerVersion" : 1,
    "namespace" : "kl.listings",
    "indexFilterSet" : false,
    "parsedQuery" : {
        "$and" : [
            {
                "dated" : {
                    "$in" : [
                        ISODate("2018-02-10T00:00:00Z"),
                        ISODate("2018-02-16T00:00:00Z"),
                        ISODate("2018-02-17T00:00:00Z")
                    ]
                }
            },
            {
                "$nor" : [{
                    "causelist_type" : {
                        "$eq" : -1
                    }
                }]
            }
        ]
    },
    "winningPlan" : {
        "stage" : "FETCH",
        "filter" : {
            "$nor" : [{
                "causelist_type" : {
                    "$eq" : -1
                }
            }]
        },
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                    "dated" : 1
            },
            "indexName" : "dated_1",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
                "dated" : [
                    "[new Date(1518220800000), new Date(1518220800000)]",
                    "[new Date(1518739200000), new Date(1518739200000)]",
                    "[new Date(1518825600000), new Date(1518825600000)]"
                ]
            }
        }
    },
    "rejectedPlans" : [{
        "stage" : "FETCH",
        "filter" : {
                "dated" : {
                        "$in" : [
                                ISODate("2018-02-10T00:00:00Z"),
                                ISODate("2018-02-16T00:00:00Z"),
                                ISODate("2018-02-17T00:00:00Z")
                        ]
                }
        },
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                    "causelist_type" : 1
            },
            "indexName" : "causelist_type_1",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
                "causelist_type" : [
                    "[MinKey, -1.0)",
                    "(-1.0, MaxKey]"
                ]
            }
        }
    }]
}
{
    "plannerVersion" : 1,
    "namespace" : "kl.listings",
    "indexFilterSet" : false,
    "parsedQuery" : {
        "dated" : {
            "$in" : [
                ISODate("2018-02-10T00:00:00Z"),
                ISODate("2018-02-16T00:00:00Z"),
                ISODate("2018-02-17T00:00:00Z")
            ]
        }
    },
    "winningPlan" : {
        "stage" : "COUNT",
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                "dated" : 1
            },
            "indexName" : "dated_1",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
            "dated" : [
                    "[new Date(1518220800000), new Date(1518220800000)]",
                    "[new Date(1518739200000), new Date(1518739200000)]",
                    "[new Date(1518825600000), new Date(1518825600000)]"
                ]
            }
        }
    },
    "rejectedPlans" : [ ]
}
{
    "plannerVersion" : 1,
    "namespace" : "kl.listings",
    "indexFilterSet" : false,
    "parsedQuery" : {
        "$and" : [{
            "causelist_type" : {
                "$eq" : -1
            }
        },
        {
            "dated" : {
                "$in" : [
                    ISODate("2018-02-10T00:00:00Z"),
                    ISODate("2018-02-16T00:00:00Z"),
                    ISODate("2018-02-17T00:00:00Z")
                ]
            }
        }]
    },
    "winningPlan" : {
        "stage" : "FETCH",
        "filter" : {
            "causelist_type" : {
                "$eq" : -1
            }
        },
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                    "dated" : 1
            },
            "indexName" : "dated_1",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
                "dated" : [
                    "[new Date(1518220800000), new Date(1518220800000)]",
                    "[new Date(1518739200000), new Date(1518739200000)]",
                    "[new Date(1518825600000), new Date(1518825600000)]"
                ]
            }
        }
    },
    "rejectedPlans" : [{
        "stage" : "FETCH",
        "filter" : {
            "dated" : {
                "$in" : [
                    ISODate("2018-02-10T00:00:00Z"),
                    ISODate("2018-02-16T00:00:00Z"),
                    ISODate("2018-02-17T00:00:00Z")
                ]
            }
        },
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                "causelist_type" : 1
            },
            "indexName" : "causelist_type_1",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
                "causelist_type" : [
                    "[-1.0, -1.0]"
                ]
            }
        }
    }]
}
explain().queryPlanner
对于不带
因果列表类型
子句的查询:

{
    "plannerVersion" : 1,
    "namespace" : "kl.listings",
    "indexFilterSet" : false,
    "parsedQuery" : {
        "$and" : [
            {
                "dated" : {
                    "$in" : [
                        ISODate("2018-02-10T00:00:00Z"),
                        ISODate("2018-02-16T00:00:00Z"),
                        ISODate("2018-02-17T00:00:00Z")
                    ]
                }
            },
            {
                "$nor" : [{
                    "causelist_type" : {
                        "$eq" : -1
                    }
                }]
            }
        ]
    },
    "winningPlan" : {
        "stage" : "FETCH",
        "filter" : {
            "$nor" : [{
                "causelist_type" : {
                    "$eq" : -1
                }
            }]
        },
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                    "dated" : 1
            },
            "indexName" : "dated_1",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
                "dated" : [
                    "[new Date(1518220800000), new Date(1518220800000)]",
                    "[new Date(1518739200000), new Date(1518739200000)]",
                    "[new Date(1518825600000), new Date(1518825600000)]"
                ]
            }
        }
    },
    "rejectedPlans" : [{
        "stage" : "FETCH",
        "filter" : {
                "dated" : {
                        "$in" : [
                                ISODate("2018-02-10T00:00:00Z"),
                                ISODate("2018-02-16T00:00:00Z"),
                                ISODate("2018-02-17T00:00:00Z")
                        ]
                }
        },
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                    "causelist_type" : 1
            },
            "indexName" : "causelist_type_1",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
                "causelist_type" : [
                    "[MinKey, -1.0)",
                    "(-1.0, MaxKey]"
                ]
            }
        }
    }]
}
{
    "plannerVersion" : 1,
    "namespace" : "kl.listings",
    "indexFilterSet" : false,
    "parsedQuery" : {
        "dated" : {
            "$in" : [
                ISODate("2018-02-10T00:00:00Z"),
                ISODate("2018-02-16T00:00:00Z"),
                ISODate("2018-02-17T00:00:00Z")
            ]
        }
    },
    "winningPlan" : {
        "stage" : "COUNT",
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                "dated" : 1
            },
            "indexName" : "dated_1",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
            "dated" : [
                    "[new Date(1518220800000), new Date(1518220800000)]",
                    "[new Date(1518739200000), new Date(1518739200000)]",
                    "[new Date(1518825600000), new Date(1518825600000)]"
                ]
            }
        }
    },
    "rejectedPlans" : [ ]
}
{
    "plannerVersion" : 1,
    "namespace" : "kl.listings",
    "indexFilterSet" : false,
    "parsedQuery" : {
        "$and" : [{
            "causelist_type" : {
                "$eq" : -1
            }
        },
        {
            "dated" : {
                "$in" : [
                    ISODate("2018-02-10T00:00:00Z"),
                    ISODate("2018-02-16T00:00:00Z"),
                    ISODate("2018-02-17T00:00:00Z")
                ]
            }
        }]
    },
    "winningPlan" : {
        "stage" : "FETCH",
        "filter" : {
            "causelist_type" : {
                "$eq" : -1
            }
        },
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                    "dated" : 1
            },
            "indexName" : "dated_1",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
                "dated" : [
                    "[new Date(1518220800000), new Date(1518220800000)]",
                    "[new Date(1518739200000), new Date(1518739200000)]",
                    "[new Date(1518825600000), new Date(1518825600000)]"
                ]
            }
        }
    },
    "rejectedPlans" : [{
        "stage" : "FETCH",
        "filter" : {
            "dated" : {
                "$in" : [
                    ISODate("2018-02-10T00:00:00Z"),
                    ISODate("2018-02-16T00:00:00Z"),
                    ISODate("2018-02-17T00:00:00Z")
                ]
            }
        },
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                "causelist_type" : 1
            },
            "indexName" : "causelist_type_1",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
                "causelist_type" : [
                    "[-1.0, -1.0]"
                ]
            }
        }
    }]
}
我正在使用MongoDB 3.6.2

我不明白的是,如果在有“过滤器”和没有“过滤器”的查询中使用相同的索引,那么添加“过滤器”怎么会使查询时间增加这么多。
如果你需要更多的信息,请告诉我

编辑:
我还尝试将
causelist_类型
子句修改为
causelist_类型:{$eq:-1}
,查询会立即运行。请解释一下

编辑2:
explain().queryPlanner
对于带有
causelist_类型:{$eq:-1}
子句的查询:

{
    "plannerVersion" : 1,
    "namespace" : "kl.listings",
    "indexFilterSet" : false,
    "parsedQuery" : {
        "$and" : [
            {
                "dated" : {
                    "$in" : [
                        ISODate("2018-02-10T00:00:00Z"),
                        ISODate("2018-02-16T00:00:00Z"),
                        ISODate("2018-02-17T00:00:00Z")
                    ]
                }
            },
            {
                "$nor" : [{
                    "causelist_type" : {
                        "$eq" : -1
                    }
                }]
            }
        ]
    },
    "winningPlan" : {
        "stage" : "FETCH",
        "filter" : {
            "$nor" : [{
                "causelist_type" : {
                    "$eq" : -1
                }
            }]
        },
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                    "dated" : 1
            },
            "indexName" : "dated_1",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
                "dated" : [
                    "[new Date(1518220800000), new Date(1518220800000)]",
                    "[new Date(1518739200000), new Date(1518739200000)]",
                    "[new Date(1518825600000), new Date(1518825600000)]"
                ]
            }
        }
    },
    "rejectedPlans" : [{
        "stage" : "FETCH",
        "filter" : {
                "dated" : {
                        "$in" : [
                                ISODate("2018-02-10T00:00:00Z"),
                                ISODate("2018-02-16T00:00:00Z"),
                                ISODate("2018-02-17T00:00:00Z")
                        ]
                }
        },
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                    "causelist_type" : 1
            },
            "indexName" : "causelist_type_1",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
                "causelist_type" : [
                    "[MinKey, -1.0)",
                    "(-1.0, MaxKey]"
                ]
            }
        }
    }]
}
{
    "plannerVersion" : 1,
    "namespace" : "kl.listings",
    "indexFilterSet" : false,
    "parsedQuery" : {
        "dated" : {
            "$in" : [
                ISODate("2018-02-10T00:00:00Z"),
                ISODate("2018-02-16T00:00:00Z"),
                ISODate("2018-02-17T00:00:00Z")
            ]
        }
    },
    "winningPlan" : {
        "stage" : "COUNT",
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                "dated" : 1
            },
            "indexName" : "dated_1",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
            "dated" : [
                    "[new Date(1518220800000), new Date(1518220800000)]",
                    "[new Date(1518739200000), new Date(1518739200000)]",
                    "[new Date(1518825600000), new Date(1518825600000)]"
                ]
            }
        }
    },
    "rejectedPlans" : [ ]
}
{
    "plannerVersion" : 1,
    "namespace" : "kl.listings",
    "indexFilterSet" : false,
    "parsedQuery" : {
        "$and" : [{
            "causelist_type" : {
                "$eq" : -1
            }
        },
        {
            "dated" : {
                "$in" : [
                    ISODate("2018-02-10T00:00:00Z"),
                    ISODate("2018-02-16T00:00:00Z"),
                    ISODate("2018-02-17T00:00:00Z")
                ]
            }
        }]
    },
    "winningPlan" : {
        "stage" : "FETCH",
        "filter" : {
            "causelist_type" : {
                "$eq" : -1
            }
        },
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                    "dated" : 1
            },
            "indexName" : "dated_1",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
                "dated" : [
                    "[new Date(1518220800000), new Date(1518220800000)]",
                    "[new Date(1518739200000), new Date(1518739200000)]",
                    "[new Date(1518825600000), new Date(1518825600000)]"
                ]
            }
        }
    },
    "rejectedPlans" : [{
        "stage" : "FETCH",
        "filter" : {
            "dated" : {
                "$in" : [
                    ISODate("2018-02-10T00:00:00Z"),
                    ISODate("2018-02-16T00:00:00Z"),
                    ISODate("2018-02-17T00:00:00Z")
                ]
            }
        },
        "inputStage" : {
            "stage" : "IXSCAN",
            "keyPattern" : {
                "causelist_type" : 1
            },
            "indexName" : "causelist_type_1",
            "isMultiKey" : false,
            "isUnique" : false,
            "isSparse" : false,
            "isPartial" : false,
            "indexVersion" : 1,
            "direction" : "forward",
            "indexBounds" : {
                "causelist_type" : [
                    "[-1.0, -1.0]"
                ]
            }
        }
    }]
}
另外,当我将
causelist_type
子句更改为
causelist_type:{$nin:[-1,-2]}
时,它是快速的,使用与
causelist_type:{$eq:-1}
相同的索引和输入阶段,只是一个不同的过滤器


它们都没有使用索引交集,正如我们所说的,在使用索引交集的情况下,我们应该使用“stage”:“And_SORTED”。

如果查询中没有
因果列表类型
项,则计数查询完全由
日期
上的索引覆盖,这使得查询非常快

请注意本案例获奖计划中的“阶段”:“计数”

causelist_type
添加到查询中需要从磁盘读取满足查询的
日期部分的每个文档,以便检查其
causelist_type
值是否为
$ne:-1

请注意本案例获奖计划中的“阶段”:“获取”


至于为什么这里没有将索引交集与
causelist_类型
索引一起使用,这可能是因为您使用的是
$ne
查询,这使得索引的效率远远低于匹配项(当您使用
$eq
进行测试时发现)。

您是否尝试过添加复合索引
{“日期”:1,“causelist_type”:1}
?@chridam No我没有。集合已经有很多索引。而且,为什么它不在
causelist_type
上提取现有索引呢?我认为MongoDB可以使用两个索引()。尝试使用
db.listings.count({dated:…,$hint:{“dated”:1})
,并让我知道执行时间。@AlejandroMontilla我认为
$hint
不适用于count()。您所说的查询返回了
未知的顶级运算符:$query
,当我尝试向查询追加
.hint()
时,它说:
db.listings.count(…).hint不是一个符合it应该工作的函数。它是在2.6版中引入的。感谢您的澄清。它确实解释了为什么没有
causelist_type
术语的查询速度如此之快。但它仍然不能解释为什么没有使用索引交集。您能看看我最近的编辑吗?