Some notes about the MONGO index

Source: Internet
Author: User
This article mainly introduced about the MONGO index of some of the notes, has a certain reference value, now share to everyone, the need for friends can refer to

Index usage Scenarios

Excellent: Faster query speed

Inferior: Add and delete will incur additional overhead, occupy space

Tips: Returns more than half of the data in a collection, with high efficiency for full table scans

Index base

Basic operations

View index: Db.test.getIndexes ()

CREATE INDEX: Db.test.ensureIndex ({"username": 1},{"Background": True, "name": "Index_test_name"})//There is a large amount of data available for background execution without blocking

Delete index: Db.test.dropIndex ({"username": 1})

View index Size: db.test.totalIndexSize ()

Property

Index Order:

1 is the positive sequence, 1 is reverse order

Order of attention in composite indexes (Id:1, age:-1)

Indexed properties:

    • Uniqueness

Db.test.ensureIndex ({x:1,y:1},{unique:true})
    • Sparse Nature

Db.test.ensureIndexx ({},{sparse:true/false}) is not sparse (default): 1. You can insert data that does not exist in the indexed field, NULL; 2. Can filter non-existent fields: Db.test.find ({m:{$exist: ture}) Sparse:

Optimization Analysis method

    • Explain

      Learn how the system handles requests

Cursor  return cursor type (basiccursor or btreecursor) nscanned  Number of documents scanned n number of documents returned Millis  time (ms) Indexbounds  Index used
    • Hint

      Force an index to be used

Db.test.find ({"Age": +}). Hint ({"name": 1, "Age": 1})//. Hint (name_1_age_1)
    • Profile

      Set the log level to record slow queries

Tips

    1. Automatic adjustment of Query condition order

    2. Can be a prefix-type regular expression hit index (/^z/)

    3. Indexing keys that require a large number of sort to prevent all data from being loaded into memory

    4. $ne, $nin do not use indexes

Type of index

    • _ID Index

    • Single-Touch indexing

    • Multi-Key indexing

    • Composite Index

    • Expired Index

    • Full-Text Indexing

    • Location Index

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

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.