【问题标题】:Improve performance of Mongodb Collection提高 Mongodb Collection 的性能
【发布时间】:2019-09-17 20:41:18
【问题描述】:

我有一个名为 Time_Cube 的集合。 下面给出的是我的文档的架构。 我想提高从此集合中获取的所有查询的性能时间。 我是 MongoDB 的新手。 您能帮我更改架构或重组查询吗?此集合中有超过 300 万条记录。

PS:这是我在 Stackoverflow 上的第一个问题 集合中的示例文档:

{
    "_id" : {
        "course" : "DPP006",
        "date" : 1506384000000,
        "course_type" : "Library",
        "browser" : "Chrome",
        "device" : "desktop",
        "os" : "Windows",
        "country" : "IN",
        "language" : "enIE",
        "site" : "ea92d3b3aeb265d60ff9a91d7ab2c041",
        "cid" : "48035"
    },
    "usersData" : [
        {
            "user" : "5c5947cc03927f7eee66339b17977153",
            "cid" : "48035"
        }
    ]
}

【问题讨论】:

    标签: mongodb


    【解决方案1】:

    如果您可以更改文档的 json 结构,则可以使用本机 _id,例如:

    {
        "_id" : ...,
        "course" : "DPP006",
        "date" : 1506384000000,
        "course_type" : "Library",
        "browser" : "Chrome",
        "device" : "desktop",
        "os" : "Windows",
        "country" : "IN",
        "language" : "enIE",
        "site" : "ea92d3b3aeb265d60ff9a91d7ab2c041",
        "cid" : "48035" ,
        "usersData" : [
            {
                "user" : "5c5947cc03927f7eee66339b17977153",
                "cid" : "48035"
            }
        ]
    }
    

    当 mongodb 将插入此类文档时,会创建一个新的唯一 _id。 从这里开始,问问自己:你在做什么查询?您的搜索花费 80% 的时间在哪里? 例如,如果您的查询都是关于“设备”的,请在其上添加索引:

    db.getCollection("Time_Cube").createIndex({ "device": 1 })

    有关如何优化查询的更多信息:https://docs.mongodb.com/manual/tutorial/analyze-query-plan/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-12-15
      • 1970-01-01
      • 2017-06-18
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多