程序员

MongoDB:创建索引需要注意的事项

2020-10-14  本文已影响0人  dex0423

1. 尽可能在写入数据前把索引创建好

2. 前台创建索引会严重影响 MongoDB

3. 尽可能的在后台创建索引

db.collectionName.create_index({'索引字段':1}, {'name': '索引的名字'}, {background:true})

4. 后台创建索引也会影响 MongoDB 的性能

5. 查看索引创建的进度的方法

db.currentOp(
    {
        $or: [
                { op: "command", "query.createIndexes": { $exists: true } },
                { op: "insert", ns: /\.system\.indexes\b/ }
            ]
    }
)
{
        "inprog" : [
                {
                        "desc" : "conn2163",
                        "threadId" : "4136",
                        "connectionId" : 2163,
                        "client" : "183.14.135.173:51330",
                        "active" : true,
                        "opid" : 1153352,
                        "secs_running" : 5075,
                        "microsecs_running" : NumberLong("5075063463"),
                        "op" : "command",
                        "ns" : "EPO.$cmd",
                        "query" : {
                                "createIndexes" : "EPO_patent",
                                "indexes" : [
                                        {
                                                "key" : {
                                                        "patent_num" : 1
                                                },
                                                "name" : "patent_num_1"
                                        }
                                ]
                        },
                        "msg" : "Index Build Index Build: 2651232/6334763 41%",     // 【注意】这里的 41% 就是创建索引的进度
                        "progress" : {
                                "done" : 2651232,
                                "total" : 6334763
                        },
                        "numYields" : 0,
                        "locks" : {
                                "Global" : "w",
                                "Database" : "W",
                                "Collection" : "w"
                        },
                        "waitingForLock" : false,
                        "lockStats" : {
                                "Global" : {
                                        "acquireCount" : {
                                                "r" : NumberLong(1),
                                                "w" : NumberLong(1)
                                        }
                                },
                                "Database" : {
                                        "acquireCount" : {
                                                "W" : NumberLong(1)
                                        },
                                        "acquireWaitCount" : {
                                                "W" : NumberLong(1)
                                        },
                                        "timeAcquiringMicros" : {
                                                "W" : NumberLong(19885)
                                        }
                                },
                                "Collection" : {
                                        "acquireCount" : {
                                                "w" : NumberLong(1)
                                        }
                                }
                        }
                }
        ],
        "ok" : 1
}
"msg" : "Index Build Index Build: 2651232/6334763 41%",
mongo { "inprog" : [ ], "ok" : 1 }

6. 要用正确方法中断创建索引进程

db.currentOp(
    {
        $or: [
                { op: "command", "query.createIndexes": { $exists: true } },
                { op: "insert", ns: /\.system\.indexes\b/ }
            ]
    }
)
"opid" : 1153352,
db.killOp(pid 进程号)

7. 创建索引时最好指定 索引 name

db.EPO_pdf.ensureIndex({'page_id':1}, {"name":"page_id"}, {background:true})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}
> db.EPO_pdf.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "EPO.EPO_pdf"
        },
        {
                "v" : 2,
                "key" : {
                        "page_id" : 1
                },
                "name" : "page_id",
                "ns" : "EPO.EPO_pdf"
        }
]
上一篇 下一篇

猜你喜欢

热点阅读