MongoDB Query optimization

Source: Internet
Author: User
Tags mongodb mongodb query

Project scenario: MONGO in the first query is very slow, the back is good. If you do not query for a long time, the next start will be very slow for the first time, so from the link at that time, to troubleshoot the final discovery or MONGO index built problems.

MongoDB often encounters long queries during large-scale data queries, often using a variety of indexes to optimize.

First step, you can use. Explain () to view the query data traces for easy analysis of the query index for each shard.

Db. Collection.find ({}). Explain ()

If you view the index, you can create a new index based on the query criteria.

1. Querying the Index

Db. Collection_name.getindexes ()

2. Create a new index

Db. Collection_name.ensureindex (Keys[options])

This command supports Shard creation index

    • keysA list of parameters to be indexed. such as: {KEY:1} , which key represents the field name, 1 indicates an ascending sort, or use numeric -1 descending.
    • options, an optional parameter that represents the setting for indexing. The optional values are as follows:
      • backgroundA Boolean that indexes in the background so that other database activities are not blocked when the index is built. The default value is False.
      • uniqueA Boolean that creates a unique index. The default value is False.
      • nameA String that specifies the name of the index. If not specified, MongoDB generates an indexed field with the name and sort order concatenation.
      • dropDups, Boolean, when a unique index is created, only the first one is retained if duplicate deletions occur for subsequent occurrences of the same index.
      • sparseA Boolean that does not enable indexing for field data that does not exist in the document. The default value is False.
      • v, index version, and the revision number of the indexes.
      • weights, document, index weight value, which is between 1 and 99,999, indicating the score weight of the index relative to the other indexed fields.

3. Re-create the index

Re-create if the index is found to be inappropriate

Db. Collection_name.reindex ()

4. Deleting an index

Db. Collection_name.dropindex ("Index-name")

MongoDB Query optimization

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.