mongodb索引的工作方式不同

时间:2018-06-14 02:14:00

标签: mongodb indexing

我不明白索引是否使用{createdAt:1}执行。

应该运行{device.objectId:1}索引吗?

有时这种情况会在很多次尝试中发生。

我知道我需要走复合索引。

但是

即使您没有复合索引,

我们知道如果条件中使用的密钥包含在单个索引中,它将搜索最佳查询执行条件。

我想知道是什么导致它起作用。

我想知道为什么会出现这个结果。

当前设置的索引。

[
    {
        "v" : 1,
        "key" : {
            "_id" : 1
        },
        "name" : "_id_",
        "ns" : "1000009494.QuestItemLog"
    },
    {
        "v" : 1,
        "key" : {
            "createdAt" : 1
        },
        "name" : "createdAt_1",
        "ns" : "1000009494.QuestItemLog",
        "background" : 1
    },
    {
        "v" : 1,
        "key" : {
            "updatedAt" : 1
        },
        "name" : "updatedAt_1",
        "ns" : "1000009494.QuestItemLog",
        "background" : 1
    },
    {
        "v" : 1,
        "key" : {
            "device.objectId" : 1
        },
        "name" : "device.objectId_1",
        "ns" : "1000009494.QuestItemLog",
        "background" : 1
    },
    {
        "v" : 1,
        "key" : {
            "content.objectId" : 1
        },
        "name" : "content.objectId_1",
        "ns" : "1000009494.QuestItemLog",
        "background" : 1
    }
]

查询日志。

2018-06-12T14:04:58.433+0900 [conn199770] query 1000009494.QuestItemLog query: { query: { $and: [ { device.objectId: "5aecd3610cf237a6838337f9" }, { content.objectId: "58f7521fc8f235c33d505e22" }, { $or: [ { ACL.5aecd3dd0cf2716ac95a6d1d.read: true }, { ACL.role:r_01035284674.read: true }, { ACL.*.read: true }, { ACL: null } ] } ] }, orderby: { createdAt: -1 }, $readPreference: { mode: "secondaryPreferred" } }
 planSummary: IXSCAN { createdAt: 1 } ntoskip:0 nscanned:7913847 nscannedObjects:7913847 keyUpdates:0 numYields:46505 locks(micros) r:173389865 nreturned:0 reslen:20 562751ms

行数

shard01:SECONDARY> db.QuestItemLog.find({"content.objectId":"58f7521fc8f235c33d505e22"}).explain();
{
    "cursor" : "BtreeCursor content.objectId_1",
    "isMultiKey" : false,
    "n" : 5095,
    "nscannedObjects" : 5095,
    "nscanned" : 5095,
    "nscannedObjectsAllPlans" : 5095,
    "nscannedAllPlans" : 5095,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 1865,
    "nChunkSkips" : 0,
    "millis" : 5236,
    "indexBounds" : {
        "content.objectId" : [
            [
                "58f7521fc8f235c33d505e22",
                "58f7521fc8f235c33d505e22"
            ]
        ]
    },
    "server" : "BPP-BAAS-MONGO104:27017",
    "filterSet" : false
}

shard01:SECONDARY> db.QuestItemLog.find({"device.objectId":"5aecd3610cf237a6838337f9"}).explain();
{
    "cursor" : "BtreeCursor device.objectId_1",
    "isMultiKey" : false,
    "n" : 153,
    "nscannedObjects" : 153,
    "nscanned" : 153,
    "nscannedObjectsAllPlans" : 153,
    "nscannedAllPlans" : 153,
    "scanAndOrder" : false,
    "indexOnly" : false,
    "nYields" : 1,
    "nChunkSkips" : 0,
    "millis" : 0,
    "indexBounds" : {
        "device.objectId" : [
            [
                "5aecd3610cf237a6838337f9",
                "5aecd3610cf237a6838337f9"
            ]
        ]
    },
    "server" : "BPP-BAAS-MONGO104:27017",
    "filterSet" : false
}

0 个答案:

没有答案