【问题标题】:Best way to create index for MongoDB为 MongoDB 创建索引的最佳方法
【发布时间】:2018-02-22 00:16:42
【问题描述】:

我将记录存储在客户的 mongo-db 集合中,并且有以下格式的交易:

{ 
"_id" : ObjectId("59b6992a0b54c9c4a5434088"), 
"Results" : {
    "id" : "2139623696", 
    "member_joined_date" : ISODate("2010-07-07T00:00:00.000+0000"), 
    "account_activation_date" : ISODate("2010-07-07T00:00:00.000+0000"),  
    "family_name" : "XYZ", 
    "given_name" : "KOKI HOI", 
    "gender" : "Female",
    "dob" : ISODate("1967-07-20T00:00:00.000+0000"), 
    "preflanguage" : "en-GB", 
    "title" : "MR", 
    "contact_no" : "60193551626", 
    "email" : "abc123@xmail.com", 
    "street1" : "address line 1", 
    "street2" : "address line 2", 
    "street3" : "address line 3", 
    "zipcd" : "123456", 
    "city" : "xyz", 
    "countrycd" : "Malaysia", 
    "Transaction" : [
        {
            "txncd" : "411", 
            "txndate" : ISODate("2017-08-02 00:00:00.000000"), 
            "prcs_date" : ISODate("2017-08-02 00:00:00.000000"), 
            "txn_descp" : "Some MALL : SHOP & FLY FREE", 
            "merchant_id" : "6587867dsfd", 
            "orig_pts" : "0.00000", 
            "text" : "Some text"
        }
    ]
}

我想在字段“txn_descp”、“txndate”、“member_joined_date”、“gender”、“dob”上创建索引以便更快地访问。有人可以帮我为这个文档创建索引吗?将不胜感激任何形式的帮助和建议。

【问题讨论】:

标签: mongodb indexing


【解决方案1】:

在创建索引时,需要牢记一些事项。

  • 始终为您使用的查询创建索引。
  • 尽可能使用复合索引。
  • 索引中的第一个字段应该是具有最小可能值的字段。即,如果有一个以性别和出生日期为键的索引,最好有{gender:1,dob:1}

【讨论】:

  • 感谢您的建议。我只是想知道我的交易相关字段在内部是分层的(结果 -> 交易 -> txndate )。查询时会影响吗?
  • 它会影响索引的大小,进而影响内存使用。关键是“Transaction.txndate”,它将占用更多空间来存储。使用中等大小的字段名称并将索引字段保持在根级别总是更好。但是,层次结构也有优势。
猜你喜欢
  • 1970-01-01
  • 2018-10-25
  • 2016-02-23
  • 1970-01-01
  • 2021-09-06
  • 1970-01-01
  • 2021-02-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多