【问题标题】:Compound MongoDB index not used for exact same query复合 MongoDB 索引未用于完全相同的查询
【发布时间】:2022-09-23 05:08:56
【问题描述】:

我有这样的索引:businessId_1_accountId_1_dataHash_1_deleted_1

而像这样的查询,不使用它,而是使用另一个。我不明白为什么。

db.getCollection(\"transactions\").find({
  \"businessId\": ObjectId(\"62c56bbdba7f1d001368f217\"),
  \"accountId\": ObjectId(\"62c56bbef2c6530d4a9b5976\"),
  \"dataHash\": \"00002478a5c9594923833b3534b2d0b17bb7298a\",
  deleted: { $ne: true }
}, { _id: true }).explain();

这是解释结果

{
    \"explainVersion\" : \"1\",
    \"queryPlanner\" : {
        \"namespace\" : \"stage.transactions\",
        \"indexFilterSet\" : false,
        \"parsedQuery\" : {
            \"$and\" : [
                {
                    \"accountId\" : {
                        \"$eq\" : ObjectId(\"62c56bbef2c6530d4a9b5976\")
                    }
                },
                {
                    \"businessId\" : {
                        \"$eq\" : ObjectId(\"62c56bbdba7f1d001368f217\")
                    }
                },
                {
                    \"dataHash\" : {
                        \"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
                    }
                },
                {
                    \"deleted\" : {
                        \"$not\" : {
                            \"$eq\" : true
                        }
                    }
                }
            ]
        },
        \"queryHash\" : \"29075D21\",
        \"planCacheKey\" : \"7C80754C\",
        \"maxIndexedOrSolutionsReached\" : false,
        \"maxIndexedAndSolutionsReached\" : false,
        \"maxScansToExplodeReached\" : false,
        \"winningPlan\" : {
            \"stage\" : \"PROJECTION_SIMPLE\",
            \"transformBy\" : {
                \"_id\" : true
            },
            \"inputStage\" : {
                \"stage\" : \"FETCH\",
                \"filter\" : {
                    \"$and\" : [
                        {
                            \"accountId\" : {
                                \"$eq\" : ObjectId(\"62c56bbef2c6530d4a9b5976\")
                            }
                        },
                        {
                            \"dataHash\" : {
                                \"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
                            }
                        }
                    ]
                },
                \"inputStage\" : {
                    \"stage\" : \"IXSCAN\",
                    \"keyPattern\" : {
                        \"businessId\" : 1.0,
                        \"reason\" : 1.0,
                        \"deleted\" : 1.0
                    },
                    \"indexName\" : \"businessId_1_reason_1_deleted_1\",
                    \"isMultiKey\" : false,
                    \"multiKeyPaths\" : {
                        \"businessId\" : [

                        ],
                        \"reason\" : [

                        ],
                        \"deleted\" : [

                        ]
                    },
                    \"isUnique\" : false,
                    \"isSparse\" : false,
                    \"isPartial\" : false,
                    \"indexVersion\" : 2.0,
                    \"direction\" : \"forward\",
                    \"indexBounds\" : {
                        \"businessId\" : [
                            \"[ObjectId(\'62c56bbdba7f1d001368f217\'), ObjectId(\'62c56bbdba7f1d001368f217\')]\"
                        ],
                        \"reason\" : [
                            \"[MinKey, MaxKey]\"
                        ],
                        \"deleted\" : [
                            \"[MinKey, true)\",
                            \"(true, MaxKey]\"
                        ]
                    }
                }
            }
        },
        \"rejectedPlans\" : [
            {
                \"stage\" : \"PROJECTION_SIMPLE\",
                \"transformBy\" : {
                    \"_id\" : true
                },
                \"inputStage\" : {
                    \"stage\" : \"FETCH\",
                    \"filter\" : {
                        \"$and\" : [
                            {
                                \"accountId\" : {
                                    \"$eq\" : ObjectId(\"62c56bbef2c6530d4a9b5976\")
                                }
                            },
                            {
                                \"dataHash\" : {
                                    \"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
                                }
                            }
                        ]
                    },
                    \"inputStage\" : {
                        \"stage\" : \"IXSCAN\",
                        \"keyPattern\" : {
                            \"businessId\" : 1.0,
                            \"tags\" : 1.0,
                            \"deleted\" : 1.0
                        },
                        \"indexName\" : \"businessId_1_tags_1_deleted_1\",
                        \"isMultiKey\" : true,
                        \"multiKeyPaths\" : {
                            \"businessId\" : [

                            ],
                            \"tags\" : [
                                \"tags\"
                            ],
                            \"deleted\" : [

                            ]
                        },
                        \"isUnique\" : false,
                        \"isSparse\" : false,
                        \"isPartial\" : false,
                        \"indexVersion\" : 2.0,
                        \"direction\" : \"forward\",
                        \"indexBounds\" : {
                            \"businessId\" : [
                                \"[ObjectId(\'62c56bbdba7f1d001368f217\'), ObjectId(\'62c56bbdba7f1d001368f217\')]\"
                            ],
                            \"tags\" : [
                                \"[MinKey, MaxKey]\"
                            ],
                            \"deleted\" : [
                                \"[MinKey, true)\",
                                \"(true, MaxKey]\"
                            ]
                        }
                    }
                }
            },
            {
                \"stage\" : \"PROJECTION_SIMPLE\",
                \"transformBy\" : {
                    \"_id\" : true
                },
                \"inputStage\" : {
                    \"stage\" : \"FETCH\",
                    \"filter\" : {
                        \"$and\" : [
                            {
                                \"accountId\" : {
                                    \"$eq\" : ObjectId(\"62c56bbef2c6530d4a9b5976\")
                                }
                            },
                            {
                                \"dataHash\" : {
                                    \"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
                                }
                            }
                        ]
                    },
                    \"inputStage\" : {
                        \"stage\" : \"IXSCAN\",
                        \"keyPattern\" : {
                            \"businessId\" : 1.0,
                            \"documentId\" : 1.0,
                            \"status\" : 1.0,
                            \"deleted\" : 1.0
                        },
                        \"indexName\" : \"businessId_1_documentId_1_status_1_deleted_1\",
                        \"isMultiKey\" : false,
                        \"multiKeyPaths\" : {
                            \"businessId\" : [

                            ],
                            \"documentId\" : [

                            ],
                            \"status\" : [

                            ],
                            \"deleted\" : [

                            ]
                        },
                        \"isUnique\" : false,
                        \"isSparse\" : false,
                        \"isPartial\" : false,
                        \"indexVersion\" : 2.0,
                        \"direction\" : \"forward\",
                        \"indexBounds\" : {
                            \"businessId\" : [
                                \"[ObjectId(\'62c56bbdba7f1d001368f217\'), ObjectId(\'62c56bbdba7f1d001368f217\')]\"
                            ],
                            \"documentId\" : [
                                \"[MinKey, MaxKey]\"
                            ],
                            \"status\" : [
                                \"[MinKey, MaxKey]\"
                            ],
                            \"deleted\" : [
                                \"[MinKey, true)\",
                                \"(true, MaxKey]\"
                            ]
                        }
                    }
                }
            },
            {
                \"stage\" : \"PROJECTION_SIMPLE\",
                \"transformBy\" : {
                    \"_id\" : true
                },
                \"inputStage\" : {
                    \"stage\" : \"FETCH\",
                    \"filter\" : {
                        \"$and\" : [
                            {
                                \"businessId\" : {
                                    \"$eq\" : ObjectId(\"62c56bbdba7f1d001368f217\")
                                }
                            },
                            {
                                \"dataHash\" : {
                                    \"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
                                }
                            }
                        ]
                    },
                    \"inputStage\" : {
                        \"stage\" : \"IXSCAN\",
                        \"keyPattern\" : {
                            \"accountId\" : 1.0,
                            \"description\" : 1.0,
                            \"address.name\" : 1.0,
                            \"deleted\" : 1.0
                        },
                        \"indexName\" : \"accountId_1_description_1_address.name_1_deleted_1\",
                        \"isMultiKey\" : false,
                        \"multiKeyPaths\" : {
                            \"accountId\" : [

                            ],
                            \"description\" : [

                            ],
                            \"address.name\" : [

                            ],
                            \"deleted\" : [

                            ]
                        },
                        \"isUnique\" : false,
                        \"isSparse\" : false,
                        \"isPartial\" : false,
                        \"indexVersion\" : 2.0,
                        \"direction\" : \"forward\",
                        \"indexBounds\" : {
                            \"accountId\" : [
                                \"[ObjectId(\'62c56bbef2c6530d4a9b5976\'), ObjectId(\'62c56bbef2c6530d4a9b5976\')]\"
                            ],
                            \"description\" : [
                                \"[MinKey, MaxKey]\"
                            ],
                            \"address.name\" : [
                                \"[MinKey, MaxKey]\"
                            ],
                            \"deleted\" : [
                                \"[MinKey, true)\",
                                \"(true, MaxKey]\"
                            ]
                        }
                    }
                }
            },
            {
                \"stage\" : \"PROJECTION_SIMPLE\",
                \"transformBy\" : {
                    \"_id\" : true
                },
                \"inputStage\" : {
                    \"stage\" : \"FETCH\",
                    \"filter\" : {
                        \"$and\" : [
                            {
                                \"businessId\" : {
                                    \"$eq\" : ObjectId(\"62c56bbdba7f1d001368f217\")
                                }
                            },
                            {
                                \"dataHash\" : {
                                    \"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
                                }
                            }
                        ]
                    },
                    \"inputStage\" : {
                        \"stage\" : \"IXSCAN\",
                        \"keyPattern\" : {
                            \"accountId\" : 1.0,
                            \"timestamp\" : 1.0,
                            \"amount\" : 1.0,
                            \"type\" : 1.0,
                            \"deleted\" : 1.0
                        },
                        \"indexName\" : \"accountId_1_timestamp_1_amount_1_type_1_deleted_1\",
                        \"isMultiKey\" : false,
                        \"multiKeyPaths\" : {
                            \"accountId\" : [

                            ],
                            \"timestamp\" : [

                            ],
                            \"amount\" : [

                            ],
                            \"type\" : [

                            ],
                            \"deleted\" : [

                            ]
                        },
                        \"isUnique\" : false,
                        \"isSparse\" : false,
                        \"isPartial\" : false,
                        \"indexVersion\" : 2.0,
                        \"direction\" : \"forward\",
                        \"indexBounds\" : {
                            \"accountId\" : [
                                \"[ObjectId(\'62c56bbef2c6530d4a9b5976\'), ObjectId(\'62c56bbef2c6530d4a9b5976\')]\"
                            ],
                            \"timestamp\" : [
                                \"[MinKey, MaxKey]\"
                            ],
                            \"amount\" : [
                                \"[MinKey, MaxKey]\"
                            ],
                            \"type\" : [
                                \"[MinKey, MaxKey]\"
                            ],
                            \"deleted\" : [
                                \"[MinKey, true)\",
                                \"(true, MaxKey]\"
                            ]
                        }
                    }
                }
            },
            {
                \"stage\" : \"PROJECTION_SIMPLE\",
                \"transformBy\" : {
                    \"_id\" : true
                },
                \"inputStage\" : {
                    \"stage\" : \"FETCH\",
                    \"filter\" : {
                        \"dataHash\" : {
                            \"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
                        }
                    },
                    \"inputStage\" : {
                        \"stage\" : \"IXSCAN\",
                        \"keyPattern\" : {
                            \"businessId\" : 1.0,
                            \"accountId\" : 1.0,
                            \"deleted\" : 1.0
                        },
                        \"indexName\" : \"businessId_1_accountId_1_deleted_1\",
                        \"isMultiKey\" : false,
                        \"multiKeyPaths\" : {
                            \"businessId\" : [

                            ],
                            \"accountId\" : [

                            ],
                            \"deleted\" : [

                            ]
                        },
                        \"isUnique\" : false,
                        \"isSparse\" : false,
                        \"isPartial\" : false,
                        \"indexVersion\" : 2.0,
                        \"direction\" : \"forward\",
                        \"indexBounds\" : {
                            \"businessId\" : [
                                \"[ObjectId(\'62c56bbdba7f1d001368f217\'), ObjectId(\'62c56bbdba7f1d001368f217\')]\"
                            ],
                            \"accountId\" : [
                                \"[ObjectId(\'62c56bbef2c6530d4a9b5976\'), ObjectId(\'62c56bbef2c6530d4a9b5976\')]\"
                            ],
                            \"deleted\" : [
                                \"[MinKey, true)\",
                                \"(true, MaxKey]\"
                            ]
                        }
                    }
                }
            },
            {
                \"stage\" : \"PROJECTION_SIMPLE\",
                \"transformBy\" : {
                    \"_id\" : true
                },
                \"inputStage\" : {
                    \"stage\" : \"FETCH\",
                    \"filter\" : {
                        \"dataHash\" : {
                            \"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
                        }
                    },
                    \"inputStage\" : {
                        \"stage\" : \"IXSCAN\",
                        \"keyPattern\" : {
                            \"businessId\" : 1.0,
                            \"accountId\" : 1.0,
                            \"vendorId\" : 1.0,
                            \"category.code\" : 1.0,
                            \"deleted\" : 1.0
                        },
                        \"indexName\" : \"businessId_1_accountId_1_vendorId_1_category.code_1_deleted_1\",
                        \"isMultiKey\" : false,
                        \"multiKeyPaths\" : {
                            \"businessId\" : [

                            ],
                            \"accountId\" : [

                            ],
                            \"vendorId\" : [

                            ],
                            \"category.code\" : [

                            ],
                            \"deleted\" : [

                            ]
                        },
                        \"isUnique\" : false,
                        \"isSparse\" : false,
                        \"isPartial\" : false,
                        \"indexVersion\" : 2.0,
                        \"direction\" : \"forward\",
                        \"indexBounds\" : {
                            \"businessId\" : [
                                \"[ObjectId(\'62c56bbdba7f1d001368f217\'), ObjectId(\'62c56bbdba7f1d001368f217\')]\"
                            ],
                            \"accountId\" : [
                                \"[ObjectId(\'62c56bbef2c6530d4a9b5976\'), ObjectId(\'62c56bbef2c6530d4a9b5976\')]\"
                            ],
                            \"vendorId\" : [
                                \"[MinKey, MaxKey]\"
                            ],
                            \"category.code\" : [
                                \"[MinKey, MaxKey]\"
                            ],
                            \"deleted\" : [
                                \"[MinKey, true)\",
                                \"(true, MaxKey]\"
                            ]
                        }
                    }
                }
            },
            {
                \"stage\" : \"PROJECTION_SIMPLE\",
                \"transformBy\" : {
                    \"_id\" : true
                },
                \"inputStage\" : {
                    \"stage\" : \"FETCH\",
                    \"filter\" : {
                        \"$and\" : [
                            {
                                \"accountId\" : {
                                    \"$eq\" : ObjectId(\"62c56bbef2c6530d4a9b5976\")
                                }
                            },
                            {
                                \"businessId\" : {
                                    \"$eq\" : ObjectId(\"62c56bbdba7f1d001368f217\")
                                }
                            }
                        ]
                    },
                    \"inputStage\" : {
                        \"stage\" : \"IXSCAN\",
                        \"keyPattern\" : {
                            \"dataHash\" : 1.0,
                            \"deleted\" : 1.0
                        },
                        \"indexName\" : \"dataHash_1_deleted_1\",
                        \"isMultiKey\" : false,
                        \"multiKeyPaths\" : {
                            \"dataHash\" : [

                            ],
                            \"deleted\" : [

                            ]
                        },
                        \"isUnique\" : false,
                        \"isSparse\" : false,
                        \"isPartial\" : false,
                        \"indexVersion\" : 2.0,
                        \"direction\" : \"forward\",
                        \"indexBounds\" : {
                            \"dataHash\" : [
                                \"[\\\"00002478a5c9594923833b3534b2d0b17bb7298a\\\", \\\"00002478a5c9594923833b3534b2d0b17bb7298a\\\"]\"
                            ],
                            \"deleted\" : [
                                \"[MinKey, true)\",
                                \"(true, MaxKey]\"
                            ]
                        }
                    }
                }
            },
            {
                \"stage\" : \"PROJECTION_SIMPLE\",
                \"transformBy\" : {
                    \"_id\" : true
                },
                \"inputStage\" : {
                    \"stage\" : \"FETCH\",
                    \"filter\" : {
                        \"$and\" : [
                            {
                                \"accountId\" : {
                                    \"$eq\" : ObjectId(\"62c56bbef2c6530d4a9b5976\")
                                }
                            },
                            {
                                \"dataHash\" : {
                                    \"$eq\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\"
                                }
                            }
                        ]
                    },
                    \"inputStage\" : {
                        \"stage\" : \"IXSCAN\",
                        \"keyPattern\" : {
                            \"businessId\" : 1.0,
                            \"address.name\" : 1.0,
                            \"status\" : 1.0,
                            \"description\" : 1.0,
                            \"vendorId\" : 1.0,
                            \"deleted\" : 1.0
                        },
                        \"indexName\" : \"businessId_1_address.name_1_status_1_description_1_vendorId_1_deleted_1\",
                        \"isMultiKey\" : false,
                        \"multiKeyPaths\" : {
                            \"businessId\" : [

                            ],
                            \"address.name\" : [

                            ],
                            \"status\" : [

                            ],
                            \"description\" : [

                            ],
                            \"vendorId\" : [

                            ],
                            \"deleted\" : [

                            ]
                        },
                        \"isUnique\" : false,
                        \"isSparse\" : false,
                        \"isPartial\" : false,
                        \"indexVersion\" : 2.0,
                        \"direction\" : \"forward\",
                        \"indexBounds\" : {
                            \"businessId\" : [
                                \"[ObjectId(\'62c56bbdba7f1d001368f217\'), ObjectId(\'62c56bbdba7f1d001368f217\')]\"
                            ],
                            \"address.name\" : [
                                \"[MinKey, MaxKey]\"
                            ],
                            \"status\" : [
                                \"[MinKey, MaxKey]\"
                            ],
                            \"description\" : [
                                \"[MinKey, MaxKey]\"
                            ],
                            \"vendorId\" : [
                                \"[MinKey, MaxKey]\"
                            ],
                            \"deleted\" : [
                                \"[MinKey, true)\",
                                \"(true, MaxKey]\"
                            ]
                        }
                    }
                }
            },
            {
                \"stage\" : \"PROJECTION_SIMPLE\",
                \"transformBy\" : {
                    \"_id\" : true
                },
                \"inputStage\" : {
                    \"stage\" : \"FETCH\",
                    \"inputStage\" : {
                        \"stage\" : \"IXSCAN\",
                        \"keyPattern\" : {
                            \"businessId\" : 1.0,
                            \"accountId\" : 1.0,
                            \"dataHash\" : 1.0,
                            \"deleted\" : 1.0
                        },
                        \"indexName\" : \"businessId_1_accountId_1_dataHash_1_deleted_1\",
                        \"isMultiKey\" : false,
                        \"multiKeyPaths\" : {
                            \"businessId\" : [

                            ],
                            \"accountId\" : [

                            ],
                            \"dataHash\" : [

                            ],
                            \"deleted\" : [

                            ]
                        },
                        \"isUnique\" : false,
                        \"isSparse\" : false,
                        \"isPartial\" : false,
                        \"indexVersion\" : 2.0,
                        \"direction\" : \"forward\",
                        \"indexBounds\" : {
                            \"businessId\" : [
                                \"[ObjectId(\'62c56bbdba7f1d001368f217\'), ObjectId(\'62c56bbdba7f1d001368f217\')]\"
                            ],
                            \"accountId\" : [
                                \"[ObjectId(\'62c56bbef2c6530d4a9b5976\'), ObjectId(\'62c56bbef2c6530d4a9b5976\')]\"
                            ],
                            \"dataHash\" : [
                                \"[\\\"00002478a5c9594923833b3534b2d0b17bb7298a\\\", \\\"00002478a5c9594923833b3534b2d0b17bb7298a\\\"]\"
                            ],
                            \"deleted\" : [
                                \"[MinKey, true)\",
                                \"(true, MaxKey]\"
                            ]
                        }
                    }
                }
            }
        ]
    },
    \"command\" : {
        \"find\" : \"transactions\",
        \"filter\" : {
            \"businessId\" : ObjectId(\"62c56bbdba7f1d001368f217\"),
            \"accountId\" : ObjectId(\"62c56bbef2c6530d4a9b5976\"),
            \"dataHash\" : \"00002478a5c9594923833b3534b2d0b17bb7298a\",
            \"deleted\" : {
                \"$ne\" : true
            }
        },
        \"projection\" : {
            \"_id\" : true
        },
        \"$db\" : \"stage\"
    },
    \"serverInfo\" : {
        \"host\" : \"cluster0-shard-00-01.u0b4v.mongodb.net\",
        \"port\" : 27017.0,
        \"version\" : \"5.0.12\",
        \"gitVersion\" : \"79cfcdd83eb6f64e164a588d0daf9bb873328b45\"
    },
    \"serverParameters\" : {
        \"internalQueryFacetBufferSizeBytes\" : 104857600.0,
        \"internalQueryFacetMaxOutputDocSizeBytes\" : 104857600.0,
        \"internalLookupStageIntermediateDocumentMaxSizeBytes\" : 16793600.0,
        \"internalDocumentSourceGroupMaxMemoryBytes\" : 104857600.0,
        \"internalQueryMaxBlockingSortMemoryUsageBytes\" : 33554432.0,
        \"internalQueryProhibitBlockingMergeOnMongoS\" : 0.0,
        \"internalQueryMaxAddToSetBytes\" : 104857600.0,
        \"internalDocumentSourceSetWindowFieldsMaxMemoryBytes\" : 104857600.0
    },
    \"ok\" : 1.0,
    \"$clusterTime\" : {
        \"clusterTime\" : Timestamp(1663107520, 28),
        \"signature\" : {
            \"hash\" : BinData(0, \"E5Mndz8gTLw14zfsCVW0yZAa9hg=\"),
            \"keyId\" : NumberLong(7083587916596772929)
        }
    },
    \"operationTime\" : Timestamp(1663107520, 28)
}

中奖计划索引为businessId_1_reason_1_deleted_1,因此仅使用businessId 进行索引。当然我可以使用 .hint(),但是……为什么会这样?我阅读了文档,但没有找到答案。它应该工作

  • mongoplayground.net 选择了人们可能期望的索引/计划,这很奇怪也很有趣。

标签: mongodb database-indexes mongodb-indexes


【解决方案1】:

准确回答这个问题需要更详细的explain 输出,即"allPlansExecution"。如果您可以提供我们可以查看的信息,我很乐意根据从中学到的任何其他意见来修改我的答案。

在没有此类信息的情况下,我们可以进行以下观察:

  1. 与任何数据库系统一样,查询规划器的环境输入非常重要。是否在具有代表生产系统的数据的环境中遇到问题?对于不包含太多数据的开发环境来说,选择“意外”计划是很常见的,但这主要是由于没有适当的信息来做出决定,而不是实际数据中会出现的问题。
  2. 此环境有很多索引,其中许多包含相似的字段。除了通常提到的索引注意事项(例如对写入吞吐量的增量影响和增加的空间使用)之外,它们还会导致优化器在规划查询时更加复杂。这可能代表review and potentially consolidate some of your indexes 的机会。
  3. 如果此查询,特别是仅检索_id 字段的投影是应用程序在实践中将执行的操作,则考虑将_id 字段附加到索引的末尾。这应该允许数据库跳过通过FETCH 检索完整文档以获得该单个值。换句话说,这样的索引将导致优化器考虑Covered Query 选项。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-31
    • 2019-03-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-12
    • 2021-01-18
    相关资源
    最近更新 更多