【问题标题】:mongodb: non-performant indexesmongodb:非性能索引
【发布时间】:2012-06-09 11:13:49
【问题描述】:

玩过之后

db.largecollection.find( { $or : [ { identifierX : "sha1_hash123" } , { identifierY : "md5_hash456" } , { identifierZ : "another_hash789" } ] } )

我检查了 mongodb 自动准备的索引。除了标识符 x/y/z 的“单个”ensureIndex 之外,现在还有一个标识符 X_1_identifierY_1_identifierZ_1,性能下降:-(

你有什么想法或提示如何向 mongodb 解释使用单个标识符的索引更快,因为我没有 $and,但是 $or 查询?

谢谢

【问题讨论】:

    标签: mongodb indexing mongodb-indexes


    【解决方案1】:

    MongoDB 不会自行创建索引。这是应用程序、用户或框架所做的事情。对于您的查询,MongoDB 只能使用 identifierX、identifierY 或 identifierZ 的索引。但是,如果您没有这样的索引,那么当然不会使用任何索引。 identifierX_1_identifierY_1_identifierZ_1 索引不能用于此查询。

    在这种情况下,您可能需要为所有这些标识符创建一个索引:

    db.ensureIndex( { 'identifierX' : 1 } );
    db.ensureIndex( { 'identifierY' : 1 } );
    db.ensureIndex( { 'identifierZ' : 1 } );
    

    MongoDB 一次只能使用一个索引,它会尝试选择“最好的”一个。尝试使用explain 来查看选择了哪个索引:

    db.largecollection.find( { $or : [
        { identifierX : "sha1_hash123" },
        { identifierY : "md5_hash456" },
        { identifierZ : "another_hash789" }
    ] } ).explain();
    

    这应该会给您一些关于正在使用哪个索引的想法。

    $or 有一个例外,MongoDB 可以为每个部分使用不同的索引并为您进行重复数据删除。它在docs 中。它(当然)仍然不使用复合索引,你需要我上面写的索引。

    【讨论】:

    • 你是对的。手动删除三重索引后,似乎没问题。 “MongoDB 一次只能使用一个索引” => 你建议我使用什么查询来查找? { $or : [ { identifierX : "sha1_hash123" } , { identifierY : "md5_hash456" } , { identifierZ : "another_hash789" } ] } 或者更好的 3 * .find 对于任何单个标识符Y/Y/ Z 和我自己合并结果?
    • 我认为您需要对自己进行基准测试。我怀疑做 3 次查询可能会更快。当然你可以很聪明,只在 identifierX 不匹配时才查询 identifierY 或 identifierZ...
    猜你喜欢
    • 2017-02-07
    • 1970-01-01
    • 1970-01-01
    • 2016-07-10
    • 2013-09-16
    • 1970-01-01
    • 2015-03-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多