Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Building Multiple Indexes at Once

Tags:

mongodb

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?

like image 526
Andrew Jones Avatar asked Feb 23 '15 03:02

Andrew Jones


People also ask

Can we create multiple indexes on same table?

A good exercise is to create multiple granular indexes (single column index) on a table, build queries using different columns in the where clause, and then view the query plan. Interestingly, SQL Server will often use the indexes as tables to reduce the work load.

Can we create multiple indexes in SQL?

SQL Server allows us to create multiple Non-clustered indexes, up to 999 Non-clustered indexes, on each table, with index IDs values assigned to each index starting from 2 for each partition used by the index, as you can find in the sys.

Is it good to have multiple indexes on a table?

It is one of the most common question about indexing: is it better to create one index for each column or a single index for all columns of a where clause? The answer is very simple in most cases: one index with multiple columns is better—that is, a concatenated or compound index.

Can you have multiple indexes?

To combine multiple indexes, the system scans each needed index and prepares a bitmap in memory giving the locations of table rows that are reported as matching that index's conditions. The bitmaps are then ANDed and ORed together as needed by the query. Finally, the actual table rows are visited and returned.


1 Answers

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"
        }
]
>
like image 118
Kevin Smith Avatar answered Oct 02 '22 13:10

Kevin Smith