【问题标题】:Is a mongodb query with 1 indexed field faster than multiple indexed fields?具有 1 个索引字段的 mongodb 查询是否比多个索引字段更快?
【发布时间】:2017-03-31 07:01:38
【问题描述】:

在以下模型中,产品归客户所有。并且不能被其他客户订购。所以我知道客户 1 的订单中只能有客户 1 拥有的产品。

这里给你一个想法是数据模型的一个简单版本:

订单:

{
  'customer' : 1
  'products' : [
  {'productId' : 'a'},
  {'productId' : 'b'}
  ]
}

产品:

{
  'id' : 'a'
  'name' : 'somename'
  'customer' : 1 
}

我需要查找包含某些产品的订单。我知道产品 ID 和客户 ID。我可以随意在我的数据库上添加/更改索引。

现在我的问题是。仅在产品 ID 上添加单个字段索引并仅使用该 ID 进行查询是否更快。还是我应该使用包含客户和产品 ID 的复合索引?

我不确定这是否重要,但在我的真实模型中,产品列表实际上是具有产品数量和 dbref 的对象列表。而且客户也是一个dbref。

这是一个完整的订单对象:

{
    "_id" : 0,
    "_class" : "nl.pfa.myprintforce.models.Order",
    "orderNumber" : "e35f1fa8-b4c4-4d53-89c9-66abe94a3553",
    "status" : "ERROR",
    "created" : ISODate("2017-03-30T11:50:50.292Z"),
    "finished" : false,
    "orderTime" : ISODate("2017-01-12T12:50:50.292Z"),
    "expectedDelivery" : ISODate("2017-03-30T11:50:50.292Z"),
    "totalItems" : 19,
    "orderItems" : [ 
        {
            "amount" : 4,
            "product" : {
                "$ref" : "product",
                "$id" : NumberLong(16)
            }
        }, 
        {
            "amount" : 7,
            "product" : {
                "$ref" : "product",
                "$id" : NumberLong(26)
            }
        }, 
        {
            "amount" : 8,
            "product" : {
                "$ref" : "product",
                "$id" : NumberLong(7)
            }
        }
    ],
    "stateList" : [ 
        {
            "timestamp" : ISODate("2017-03-28T11:50:50.074Z"),
            "status" : "NEW",
            "message" : ""
        }, 
        {
            "timestamp" : ISODate("2017-03-29T11:50:50.075Z"),
            "status" : "IN_PRODUCTION",
            "message" : ""
        }, 
        {
            "timestamp" : ISODate("2017-03-30T11:50:50.075Z"),
            "status" : "ERROR",
            "message" : "Something went wrong"
        }
    ],
    "customer" : {
        "$ref" : "customer",
        "$id" : ObjectId("58dcf11a71571a24c475c044")
    }
}

【问题讨论】:

    标签: mongodb indexing


    【解决方案1】:

    当我有以下索引时:

    1: {"customer" : 1, "orderItems.product" : 1}
    2: {"orderItems.product" : 1}
    

    两个count查询(我用count强行查找所有没有网络传输的文档):

    a: db.getCollection('order').find({
    'orderItems.product' : DBRef('product',113)
    }).count()
    
    b: db.getCollection('order').find({
    'customer' : DBRef('customer',ObjectId("58de009671571a07540a51d5")),
    'orderItems.product' : DBRef('product',113)
    }).count()
    

    在一组 200k 上以约 0.007 秒的相同时间运行。 当我为不同的客户(和不同的产品)添加 1000k 记录时,它根本不会影响时间。

    一个扩展的解释表明:

    查询 1 只使用索引 2。 查询 2 使用了索引 2,但也考虑了索引 1。也许这里使用了索引交集?

    因为如果我删除索引 1,结果是:

    查询a:0.007秒 查询 b:0.035 秒(长 5 倍!)

    所以我的结论是,通过正确的索引,这两种方法的工作速度都一样快。但是,如果您不需要复合索引来做其他事情,那只会浪费空间和写入速度。

    所以:在我的情况下,单字段索引更好

    【讨论】:

      猜你喜欢
      • 2020-10-11
      • 1970-01-01
      • 1970-01-01
      • 2016-10-24
      • 1970-01-01
      • 2016-01-18
      • 2012-11-16
      • 1970-01-01
      • 2019-09-13
      相关资源
      最近更新 更多