MongoDB Authoritative Guide Learning Note 5---Index-related knowledge points

Source: Internet
Author: User

1 Viewing the query plan

Db.user.find ({"username": "xxx"}) . Explain ()

Db.doc.find ({"Es_y": "}"). Explain ()
{
"Cursor": "Basiccursor",
"Ismultikey": false,
"N": 0,
"Nscannedobjects": 1,
"Nscanned": 1,
"Nscannedobjectsallplans": 1,
"Nscannedallplans": 1,
"Scanandorder": false,
"IndexOnly": false,
"Nyields": 0,
"Nchunkskips": 0,
"Millis": 0,
"Server": "idc254:27017",
"Filterset": false
}

--------

IndexOnly---Indicate whether the required fields can be returned only with an index, not two times according to the address of the document!

Cursor Indicates whether the index is used

Nscanned is the total number of documents that MONGODB scanned to complete this query.

Millis shows the number of milliseconds that the query is consuming.

N shows the number of query results.

Scanandorder---Indicate if you need to sort in memory!

2 building an index

Db.user.ensureIndex ({"username": 1})

3 Querying all indexes of a collection

Db.doc.getIndexes ()

4 Viewing the operation of the current server

Db.currentop ()

{
"InProg": [
{
"Opid": 486,
"Active": true,
"Secs_running": 2,
"Op": "Getmore",
"NS": "Local.oplog.rs",
"Query": {

},
"Client": "192.168.56.66:37299",
"desc": "Conn2",
"ThreadId": "0x7f1e191d7700",
"ConnectionID": 2,
"Waitingforlock": false,
"Numyields": 0,
"Lockstats": {
"Timelockedmicros": {
"R": Numberlong (89),
"W": Numberlong (0)
},
"Timeacquiringmicros": {
"R": Numberlong (15),
"W": Numberlong (0)
}
}
}
]
}

5 About Indexes

For write operations slow, a collection of up to 64 indexes, usually not more than 2 index number!

6 Creating a composite index

Db.user.ensureIndex ({"Key1": 1, "Key2": 1})

7 Memory Sequencing

If you need to sort in memory and exceed 32MB.

You will get an error!

8 forcing the specified index to query

. Hint ({index ... })

9 Overwrite Index

Simply put, the indexed field is ready to meet the requirements, no more than two times to fetch documents according to the address!

10 Implicit indexing

For example {"Age": 1, "username": 1} can be used as {"Age": 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.