MongoDB index Creation (5)

Source: Internet
Author: User

Index creation

1: Indexes increase query speed, reduce write speed, weigh common query fields and do not have to be indexed on too many columns

2. In MongoDB, indexes can be created by word orderby order/descending order, easy to sort

3. The default is to use Btree to organize the index file, after the 2.4 version, it is also allowed to establish a hash index.

View query Plans

Db.find (query). explain ();

"Cursor": "Basiccursor",----description No index works

"Nscannedobjects":---Theoretically how many rows to scan

Cursor ":" Btreecursor sn_1 ", btree index used

Index Common commands:

View Current index Status: Db.collection.getIndexes ();

Db.articles.getIndexes ();

Create a normal single-column index: Db.collection.ensureIndex ({field:1/-1}); 1 is the renewal 2 is the continuation

Db.articles.ensureIndex ({title:1})

Delete a single index

Db.collection.dropIndex ({filed:1/-1});

Delete all indexes

Db.collection.dropIndexes ();

Creating Multi-column indexes

Db.collection.ensureIndex ({field1:1/-1, field2:1/-1});

Create sub-document index

Db.collection.ensureIndex ({filed.subfield:1/-1});

To create a unique index:

Db.collection.ensureIndex ({filed.subfield:1/-1}, {unique:true});

To create a sparse index:

Features of sparse indexes------If you index a field, the index is not indexed for documents that do not contain field columns.

The normal index, in contrast, considers the value of the field column of the document NULL, and builds the index.

Appropriate: When a small number of documents contain a column.

Db.collection.ensureIndex ({field:1/-1},{sparse:true});

> Db.tea.find ();

{"_id": ObjectId ("5275f99b87437c610023597b"), "email": "[email protected]"}

{"_id": ObjectId ("5275f99e87437c610023597c"), "email": "[email protected]"}

{"_id": ObjectId ("5275f9e887437c610023597e"), "email": "[email protected]"}

{"_id": ObjectId ("5275fa3887437c6100235980")}

As above, the last line does not have an email column,

If you add normal indexes, and sparse indexes separately,

The last line of email is treated as null and ignores the last line.

Query according to {email:null}, the former can be found, and the sparse index cannot find the last line.

Create a hash index (2.4 new)

The hash index is faster than a normal index, but cannot be optimized for a range query.

Suitable for---random and strong hashing

Db.collection.ensureIndex ({file: ' hashed '});

Rebuilding indexes

A table has been modified many times, causing the table's file to be empty, as is the index file.

You can reduce the index file fragmentation and improve the efficiency of the index by rebuilding the index.

Optimize table similar to MySQL

Db.collection.reIndex ()

MongoDB index Creation (5)

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.