Building Multiple Indexes at Once

ⅰ亾dé卋堺 提交于 2021-02-08 12:27:34

问题


I have a need to build five indexes in a large MongoDB collection. I'm familiar with the ensureIndex operation, but I do not know of a way to create all five of the indexes with a single command. Is this batch index creation possible in MongoDB?


回答1:


This is pretty simple within the shell, there is a extention to the collection of createIndexes and you just pass in the keys you wish to create indexes on.

db.test.createIndexes([
        { "a" : 1 },
        { "b" : 1 },
        { "c" : 1 },
        { "d" : 1 },
        { "e" : 1 }
    ]);

This will then give us the following

> db.test.getIndexes()
[
        {
                "v" : 2,
                "key" : {
                        "_id" : 1
                },
                "name" : "_id_",
                "ns" : "test.test"
        },
        {
                "v" : 2,
                "key" : {
                        "a" : 1
                },
                "name" : "a_1",
                "ns" : "test.test"
        },
        {
                "v" : 2,
                "key" : {
                        "b" : 1
                },
                "name" : "b_1",
                "ns" : "test.test"
        },
        {
                "v" : 2,
                "key" : {
                        "c" : 1
                },
                "name" : "c_1",
                "ns" : "test.test"
        },
        {
                "v" : 2,
                "key" : {
                        "d" : 1
                },
                "name" : "d_1",
                "ns" : "test.test"
        },
        {
                "v" : 2,
                "key" : {
                        "e" : 1
                },
                "name" : "e_1",
                "ns" : "test.test"
        }
]
>



回答2:


You are wrong, Mongo has createIndexes command since 2.6 (released before 2014)

https://docs.mongodb.com/v3.0/reference/command/createIndexes/

Documentation says, that it requires one pass through collection, so it should be approximately 5 times faster.




回答3:


As for now there is no solution for that.

Background will prevent the database from locking and will allow to perform other operations. But to run those operations you will have to open new mongo shell or run them asynchronously in the language of your choice (like js).

But if you need a strong consistency and don't need background index building... you probably will have to wait for a MongoDB native solution to come.




回答4:


I think this is not possible using single command, but you can create your own script which'll perform same. If your collection size is large then I suggest you to build indexes separately with background true to reduce chances of any problem of locking.

db.collection.ensureIndex( { a: 1 }, { background: true } )


来源:https://stackoverflow.com/questions/28666570/building-multiple-indexes-at-once

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!