MongoDB Finishing Notes Index

Source: Internet
Author: User

MongoDB provides a variety of index support, the index information is saved in System.indexes, and the default is always to create an index for _ID, its index uses the basic and MySQL and other relational database. In fact, it can be said that the index is above the data storage system on the other layer of the system, so the various structures of different storage have the same or similar index implementation and use of interfaces and not surprisingly.

Base Index

Create an index on the age of the field, 1 (ascending);-1 (descending)

> Db.t3.ensureIndex ({age:1})> db.t3.getIndexes (); [  {"name": "_id_","ns": "Test.t3","key" : {"_id": 1},"V": 0},{"_id": ObjectId ("4fb906da0be632163d0839fe"),"ns": "Test.t3","key" : {"age": 1}," Name ":" Age_1 "," V ": 0}]>
View Code

The previous example shows a total of 2 indexes, where _id is an index created automatically when the table is created and cannot be deleted. When the system already has a lot of data, creating an index is a very time-consuming activity, we can execute it in the background, just specify "Backgroud:true".

> Db.t3.ensureIndex ({age:1}, {backgroud:true})

Document Index
Indexes can be any type of field, even documents

Db.factories.insert ({name: "WWL", addr: {city: ' Beijing ', state: "BJ"}});

Create an index on the addr column

Db.factories.ensureIndex ({addr:1});

The following query will use the index we just created

Db.factories.find ({addr: {city: ' Beijing ', state: ' BJ '}});

However, the following query will not use the index because the order of the queries is different from the order in which the indexes are established

Db.factories.find ({addr: {state: ' BJ ', City: ' Beijing '}});

Combined Index

As with other database products, MongoDB also has a composite index, and below we will build the combined index on addr.city and addr.state. When creating a composite index, the 1 after the field indicates ascending, 1 means descending, 1 or 1 is mainly related to the time of sorting or querying within the specified range.

Db.factories.ensureIndex ({"addr.city": 1, "Addr.state": 1});

This index is used in the following query.

Db.factories.find ({"addr.city": "Beijing", "addr.state": "BJ""addr.city": "Beijing""addr.city": 1 , "Addr.state": 1"addr.city": 1})

Unique index
You can create a unique index by simply specifying "Unique:true" in the Ensureindex command. For example, insert 2 records into the table T4

Db.t4.insert ({firstname: "Wang", LastName: "Wenlong""Wang", LastName: "Wenlong"});

Create a unique index in the T4 table

True"Wang",:"Wenlong"}

As you can see, when a unique index is built, the system reports a "duplicate value in table" error, because the table has 2 one-mode one-mode data, so the unique index cannot be established.

force the use of indexes

The hint command can force an index to be used.

> Db.t5.insert ({name: "Wangwenlong", age:20})> Db.t5.ensureIndex ({name:1, age:1})> Db.t5.find ({age:{$lt: 30}}). Explain () {"Cursor": "Basiccursor","Nscanned": 1,"Nscannedobjects": 1,"N": 1,"Millis": 0,"Nyields": 0,"Nchunkskips": 0,"Ismultikey":false,"IndexOnly":false,"Indexbounds": {--does not use the index}}> Db.t5.find ({age:{$lt: +}). Hint ({name:1, age:1}). Explain ()--force the use of index {"Cursor": "Btreecursor name_1_age_1","Nscanned": 1,"Nscannedobjects": 1,"N": 1,"Millis": 1,"Nyields": 0,"Nchunkskips": 0,"Ismultikey":false,"IndexOnly":false,"Indexbounds": {--the index is forced to use"Name" : [[{"$minElement": 1},{"$maxElement": 1}]],"Age" : [[-1.7976931348623157e+308,30]]}}>
View Code

Delete Index

Deleting an index is divided into deleting all indexes on a table and deleting an index of a table, as follows:
Delete all indexes in the T3 table

Db.t3.dropIndexes ()

Remove the FirstName index from the T4 table

Db.t4.dropIndex ({firstname:1})

Explain execution plan

MongoDB provides a explain command to let us know how the system handles query requests. With the explain command, we can look at how the system uses indexes to speed up retrieval, while optimizing indexes in a targeted way.

> Db.t5.ensureIndex ({name:1})> Db.t5.ensureIndex ({age:1})> Db.t5.find ({age:{$gt:}}, {Name:1}). Explain () {"Cursor": "Btreecursor age_1","Nscanned": 0,"Nscannedobjects": 0,"N": 0,"Millis": 0,"Nyields": 0,"Nchunkskips": 0,"Ismultikey":false,"IndexOnly":false,"Indexbounds" : {"Age" : [[45,1.7976931348623157e+308]]}}
View Code

Field Description:
Cursor: Returns the cursor type (basiccursor or btreecursor)
nscanned: Number of documents scanned
N: Number of documents returned
Millis: Time-consuming (milliseconds)
Indexbounds: The index used

MongoDB Finishing Notes Index

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.