MongoDB 深度数组扫描:多键复合索引
MongoDB deep array scan: multikey compound indexing
我收集了客户及其访问过的地点,整理如下:
{
"customer_id": 151,
"first_name": "Nakia",
"last_name": "Boyle",
"visited_places": [
{
"country": "Portugal",
"cities": [
"South Nicklausburgh",
"East Graham"
]
},
{
"country": "Rwanda",
"cities": [
"West Kristofer",
"Effertzbury",
"Stokeston",
"South Darionfort",
"Lewisport"
]
}
]
}
我正在尝试查找访问过特定国家/地区特定城市的所有客户。
查询对象为:
{
"visited_places.country" : "Portugal",
"visited_places.cities" : "South Nicklausburgh"
}
这个查询的理想索引是什么?
我试图像这样创建一个复合索引:
collection.createIndex({
'visited_places.cities': 1,
'visited_places.country': 1
}
确实使用了这个索引,但仅用于查找城市,正如执行计划在 IXSCAN 阶段解释的那样:
"indexBounds": {
"visited_places.cities": [
"[\"South Nicklausburgh\", \"South Nicklausburgh\"]"
],
"visited_places.country": [
"[MinKey, MaxKey]"
]
在后续的 FETCH 阶段过滤掉国家:
"filter": {
"visited_places.country": {
"$eq": "Portugal"
}
}
为什么仅从复合索引无法完成查询,这种模式和查询理想的索引是什么?
像这样使用 $elemMatch
db.collection.find({
"visited_places": {
"$elemMatch": {
"country": "Portugal",
"cities": {
"$elemMatch": {
"$eq": "South Nicklausburgh"
}
}
}
}
},
)
我收集了客户及其访问过的地点,整理如下:
{
"customer_id": 151,
"first_name": "Nakia",
"last_name": "Boyle",
"visited_places": [
{
"country": "Portugal",
"cities": [
"South Nicklausburgh",
"East Graham"
]
},
{
"country": "Rwanda",
"cities": [
"West Kristofer",
"Effertzbury",
"Stokeston",
"South Darionfort",
"Lewisport"
]
}
]
}
我正在尝试查找访问过特定国家/地区特定城市的所有客户。 查询对象为:
{
"visited_places.country" : "Portugal",
"visited_places.cities" : "South Nicklausburgh"
}
这个查询的理想索引是什么? 我试图像这样创建一个复合索引:
collection.createIndex({
'visited_places.cities': 1,
'visited_places.country': 1
}
确实使用了这个索引,但仅用于查找城市,正如执行计划在 IXSCAN 阶段解释的那样:
"indexBounds": {
"visited_places.cities": [
"[\"South Nicklausburgh\", \"South Nicklausburgh\"]"
],
"visited_places.country": [
"[MinKey, MaxKey]"
]
在后续的 FETCH 阶段过滤掉国家:
"filter": {
"visited_places.country": {
"$eq": "Portugal"
}
}
为什么仅从复合索引无法完成查询,这种模式和查询理想的索引是什么?
像这样使用 $elemMatch
db.collection.find({
"visited_places": {
"$elemMatch": {
"country": "Portugal",
"cities": {
"$elemMatch": {
"$eq": "South Nicklausburgh"
}
}
}
}
},
)