MongoDB Getting Started Learning (iv): MongoDB index

Source: Internet
Author: User
Tags bulk insert

The previous article on the basic operation of MongoDB and add and change, for the query, must follow our query requirements to go to the collection, and will find the results returned, in this process is actually the entire collection of each document is scanned, if meet our requirements to add to the result set to the final return. For a small collection, this process is nothing, but the collection of data is very large, the table scan is a very scary thing, so the index is used to speed up the query, the equivalent of the book directory, with the directory can be very precise positioning to find the location of the content, thus reducing unnecessary search.

1. Types of indexes

You can create an index on a single field, or on multiple fields, which is selected according to your actual situation, and the order of the fields is also fastidious when you create the index. The index is created through the Ensureindex () method, which requires that the method pass a document as the data, where the fields and order of the index are specified, 1 for ascending, and 1 for descending.

1). Default Index

Remember "_id", the data of this field cannot be duplicated, it is the default index of MongoDB, and cannot be deleted.

2). Single-column index

An index created on a single field is a single-column index, which can be used to accelerate queries to that key during a query, but it is not helpful to query other keys. The order of the single-column indexes does not affect the query on that key, creating a single-column index:

> Db.people.ensureIndex ({"Name": 1})

3). Combined Index

You can also create a composite index on multiple keys, where the position of the key and the order of the indexes affect the efficiency of the query, as seen below to create a composite index:

> Db.people.ensureIndex ({"Name": 1, "Age": 1}) > Db.people.ensureIndex ({"Age": 1, "name": 1})

The first case would be to sort the name of the organization, and when name is the same as when the age is sorted, so the {"name": 1} and {"Name": 1, "Age": 1} of the query more efficient, and the second case of the age sort, when age again to the name, so {"age": 1} and {"Age": 1, "name": 1} queries are more efficient. When a composite index contains many fields, the query for the first few keys is helpful.

4). Inline Document Index

You can also create an index on an embedded document, just as you would create an index on a normal key, or you can create a composite index on an inline document:

> Db.people.ensureIndex ({"Friends.name": 1}) > Db.people.ensureIndex ({"Friends.name": 1, "Friends.age": 1})

take a look at several other forms of indexing:

Unique index > Db.people.ensureIndex ({"Name": 1}, {"Unique": true}) > Db.people.ensureIndex ({"Name": 1}, {"Unique": true, "Dropdups": true}) loosely indexed > Db.people.ensureIndex ({"Name": 1}, {"Sparse": true}) multivalued index > Db.people.find () {"Name": ["Ma Ry "," Rose "]}> Db.people.ensureIndex ({" Name ": 1})

A unique index uniquely guarantees that the value corresponding to the key is unique in the collection, and if a unique index is created, the field will have duplicate data, then the creation fails, and the dropdups field can be added to eliminate duplicate data, which preserves the first document found. Other documents that have duplicate data will be deleted.

Some of the documents in the collection do not have some fields, or the value of some fields is null, then we do not want to let these null-valued documents participate when we create the index on this field, then we define the sparse index, for example, when we create an index on name, we find that some people only have school number and no name in the database. Then we do not want to include them, which is defined as loosely indexed.

The value of a key is an array that, when created on the key, is a multivalued index that generates an index element for each value in the array, equivalent to splitting into several separate index entries, but they still correspond to the same document data.

2. Management of indexes

Indexes are created for queries and can be indexed for each key, but indexes require storage space, so the index is not as much as possible, and after the index is created, each insert, update, and delete document will incur additional overhead because the database does not only perform these operations, You also mark these operations in the collection index. So to create an index based on the actual situation, the index is useless and then deleted.

Creating an index is the Ensureindex () method, which allows you to view the index created in the collection by Getindexes () when it is created:

> Db.people.ensureIndex ({"Name": 1, "Age": 1}) > Db.people.getIndexes () [        {                "V": 1,                "key": {                        "_id ": 1                },                " ns ":" Test.people ","                name ":" _id_ "        },        {                " V ": 1,                " key ": {                        " name ": 1,                        "Age": 1                },                "ns": "Test.people",                "name": "Name_1_age_1"        }]

You can see that the people collection creates two indexes, one is "_id", this is the default index, and the other is a combined index of name and age, with the name keyname1_dir_keyname2_dir_...,keyname the key for the index, dir for direction , 1 stands for Ascending,-1 for descending. Of course we can also customize the name of the index:

> Db.people.ensureIndex ({"Name": 1, "Age": 1}, {"Name": "Myindex"}) > Db.people.getIndexes () [        {                "V": 1,
    "key": {                        "_id": 1                },                "ns": "Test.people",                "name": "_id_"        },        {                "V": 1,                "key": {
    "name": 1,                        "age": 1                },                "ns": "Test.people",                "name": "Myindex"        }]

Delete Index is via Dropindex ():

Mode one:> db.people.dropIndex ({"Name": 1, "Age": 1}) {"Nindexeswas": 2, "OK": 1} Way two:> Db.runcommand ({"Dropindexes" : "People", "index": "Myindex"}) {"Nindexeswas": 2, "OK": 1}

The meta information for the index is stored in the System.indexes collection of each database and cannot be inserted and deleted, only through Ensureindex and Dropindex.

> Db.system.indexes.find () {"V": 1, "key": {"_id": 1}, "ns": "Test.people", "name": "_id_"} {"V": 1, "key": { "Name": 1, "Age": 1}, "ns": "Test.people", "name": "Myindex"}

Emptying all documents in the collection does not delete the index, the original created index still exists, but the collection is deleted if the collection is dropped directly.

3. Efficiency of the Index

If we define a lot of indexes, MongoDB will reorder according to our query options and intelligently choose an optimal one to use, such as we created {"Name": 1, "Age": 1} and {"Age": 1, "Class": 1} Two indexes, But our query item is find ({"Age": Ten, "Name": "Mary"}), then MongoDB will automatically reorder to find ({"name": "Mary", "Age": 10}), and use the index {"name": 1, "Age" : 1} to query.

MongoDB provides explain tools to help us get a lot of useful information about queries, as long as you call this method on the cursor to get the details of the query. Add 10W documents to the math collection below to see the efficiency comparison before and after using the index:

> var arr = [];> for (var i = 0; i < 100000; i++) {... var doc = {}; var value = Math.floor (Math.random () * 1000 );... doc["number"] = value, .... arr.push (DOC); ...} 100000> Db.math.insert (arr) > Db.math.count () 100000> db.math.find (). Limit () {"_id": ObjectId (" 53a7f7c6e4fd24348ce61fe5 ")," number ": 462} {" _id ": ObjectId (" 53a7f7c6e4fd24348ce61fe6 ")," number ": 123} {" _id ": Obje CtId ("53a7f7c6e4fd24348ce61fe7"), "number": *} {"_id": ObjectId ("53a7f7c6e4fd24348ce61fe8"), "number": ~ {"_id": ObjectId ("53a7f7c6e4fd24348ce61fe9"), "number": 244} {"_id": ObjectId ("53a7f7c6e4fd24348ce61fea"), "number": 972} {"_  ID ": ObjectId (" 53a7f7c6e4fd24348ce61feb ")," number ": 925} {" _id ": ObjectId (" 53a7f7c6e4fd24348ce61fec ")," number ": 110  } {"_id": ObjectId ("53a7f7c6e4fd24348ce61fed"), "number": 739} {"_id": ObjectId ("53a7f7c6e4fd24348ce61fee"), "number" : 945}

Add 10W data to the ARR array with a For loop, and then bulk insert the data into the math collection to see the first 10 data, because it is the resulting value, so the value of the Number field will have duplicate values, and we will query the value 462:

:> Db.math.find ({"Number": 462}) before creating the index. Explain () {"cursor": "Basiccursor", "Ismultikey": false,        "N": 94, "Nscannedobjects": 100000, "nscanned": 100000, "Nscannedobjectsallplans": 100000, "Nscannedallplans": 100000, "Scanandorder": false, "indexOnly": false, "Nyields": 0, "nCh Unkskips ": 0," Millis ": +," Indexbounds ": {}," server ":" server0.169:9352 "} After creating the index:> DB        . Math.ensureindex ({"Number": 1}) > Db.math.find ({"Number": 462}). Explain () {"cursor": "Btreecursor number_1", "Ismultikey": false, "n": 94, "Nscannedobjects": 94, "nscanned": 94, "Nscannedobjec Tsallplans ": 94," Nscannedallplans ": 94," Scanandorder ": false," indexOnly ": false," Nyiel                        DS ": 0," nchunkskips ": 0," Millis ": 0," Indexbounds ": {" number ": [             [                   462, 462]]}, "Ser Ver ":" server0.169:9352 "}

Here is a look at the useful information, "cursor" refers to which index, "nscanned" represents how many documents were found, "n" refers to the number of returned documents, "Millis" represents the time spent in the query, in milliseconds. It can be seen that the index was not used before the index was created, and it took 35 milliseconds to query in all the documents, and after the index was created, the index was number_1 indexed, and the indexes were stored in the B-tree structure and queried in only 94 documents, with little time spent.

If there are many indexes, MongoDB will automatically choose one to query, you can also use hint to force an index, here is forced to use {"Age": 1, "name": 1} This index:

> Db.people.find ({"Age": {"$GT": Ten}, "name": "Mary"}). Hint ({"Age": 1, "name": 1})

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.