MongoDB Study Notes (INDEX)

Source: Internet
Author: User

I. indexing basics:
MongoDB indexes are almost identical to those of traditional relational databases, which also includes some basic optimization techniques. The following command creates an index:
> Db. test. ensureIndex ({"username": 1 })
You can use the following name to check whether the index has been successfully created:
> Db. test. getIndexes ()
The command to delete an index is:
> Db. test. dropIndex ({"username": 1 })
In MongoDB, we can also create a composite index, for example:
-- Number 1 indicates that the index of the username key is stored in ascending order, and-1 indicates that the index of the age key is stored in descending order.
> Db. test. ensureIndex ({"username": 1, "age":-1 })
After the index is created, the index will be used for queries based on username and age, or for queries based on username, however, this composite index is not used for only age-based queries. Therefore, to use a composite index, you must include the first n index columns in the composite index in the query conditions. However, if the key-Value Order in the query condition is inconsistent with the order in which the composite index is created, MongoDB can help us adjust the order so that the composite index can be used for query. For example:
> Db. test. find ({"age": 30, "username": "stephen "})
For the query conditions in the preceding example, MongoDB dynamically adjusts the order of the query condition documents before retrieval, so that the query can use the compound index just created.
We can create indexes for embedded documents. There is no difference between the rules and common documents, for example:
> Db. test. ensureIndex ({"comments. date": 1 })
For the index created above, MongoDB will automatically assign an index name based on the index's keyname and index direction. The following command can specify the index name when creating the index, for example:
> Db. test. ensureIndex ({"username": 1 },{ "name": "testindex "})
As the set grows, You Need To index a large number of orders in the query. If sort is not called for the index key, MongoDB needs to extract all data to the memory and sort it. Therefore, if the data volume is too large to be sorted in the memory during index-free sorting, an error will be reported in MongoDB.

Ii. Unique index:
By default, the created indexes are not unique indexes. The following example creates a unique index, for example:
> Db. test. ensureIndex ({"userid": 1 },{ "unique": true })
If a duplicate userid document is inserted again, MongoDB reports an error, prompting you to insert a duplicate key, for example:
> Db. test. insert ({"userid": 5 })
> Db. test. insert ({"userid": 5 })
E11000 duplicate key error index: test. test. $ userid_1 dup key: {: 5.0}
If the inserted document does not contain the userid key, the key value in this document is null. If you insert a similar document multiple times, MongoDB will report the same error, for example:
> Db. test. insert ({"userid1": 5 })
> Db. test. insert ({"userid1": 5 })
E11000 duplicate key error index: test. test. $ userid_1 dup key: {: null}
If duplicate items already exist when creating a unique index, we can use the following command to help us eliminate duplicate documents when creating a unique index. Only the first document found is retained, for example:
-- Delete the created unique index first.
> Db. test. dropIndex ({"userid": 1 })
-- Insert test data to ensure that duplicate keys exist in the set.
> Db. test. remove ()
> Db. test. insert ({"userid": 5 })
> Db. test. insert ({"userid": 5 })
-- Create a unique index and eliminate duplicate data.
> Db. test. ensureIndex ({"userid": 1 },{ "unique": true, "dropDups": true })
-- The query result confirms that duplicate keys are indeed deleted when the index is created.
> Db. test. find ()
{"_ Id": ObjectId ("4fe823c180144abd15acd52e"), "userid": 5}

We can also create a unique compound index, that is, ensure that the compound key value is unique. For example:
> Db. test. ensureIndex ({"userid": 1, "age": 1 },{ "unique": true })

Iii. Use explain:
Explain is a very useful tool that can help you obtain a lot of useful information about queries. You can call this method on the cursor to obtain the query details. Explain returns a document instead of the cursor itself. For example:
> Db. test. find (). explain ()
{
"Cursor": "BasicCursor ",
"Nscanned": 1,
"NscannedObjects": 1,
"N": 1,
"Millis": 0,
"NYields": 0,
"NChunkSkips": 0,
"IsMultiKey": false,
"IndexOnly": false,
"IndexBounds ":{

}
}
Explain returns the index used for the query, the time consumed, and the number of scanned documents.
"Cursor": "BasicCursor" indicates that no index is used.
"Nscanned": 1 indicates how many documents are queried.
"N": 1 indicates the number of returned documents.
"Millis": 0 indicates the time consumed by the entire query.

Iv. index management:
The system. indexes set contains detailed information about each index. Therefore, you can use the following command to query existing indexes, for example:
> Db. system. indexes. find ()
If you create an index for an existing document, you can execute the following command to create an index for MongoDB in the background, so that other operations are not blocked during the creation. However, creating indexes in blocking mode will make the entire creation process more efficient, but MongoDB cannot receive other operations during creation.
> Db. test. ensureIndex ({"username": 1 },{ "background": true })

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.