[MongoDB] Eight optimization skills and eight MongoDB skills

Source: Internet
Author: User

[MongoDB] Eight optimization skills and eight MongoDB skills
Tip 1: Minimize disk access to memory much faster than disk access. Therefore, the essence of optimization is to minimize disk access.
The read speed of the memory is 1 million times faster than that of the disk. Reading a disk takes a long time. Several Simple Methods: SSD (solid state drive); increased memory can reduce hard disk reading, so that common documents are stored in the memory; tip 2: Using indexes to reduce memory usage indexes is ordered, so you do not have to traverse all items. When querying, the system first checks the index results and loads qualified documents into the memory to reduce memory usage. Tip 3: Do not use indexes everywhere. Note that not all queries can use indexes. Indexes are generally used when the returned results are only a small part of the overall data. Based on experience, once we need to return about half of the data in the set, we should not use indexes.
If you have already created an index for a field and want not to use it for large-scale fuzzy queries (because it may be inefficient to use), you can use natural sorting with {"$ natural ": 1} to force mongodb to disable the index. The natural sorting means that "data is returned according to the storage order on the disk", so that mongodb will not use the index. Tip 4: index overwrite query if only some fields are returned and all these fields can be placed in the index, mong odb can perform index overwrite query ), this type of query does not access the document pointed to by the pointer. Second, the results are directly returned using the indexed data.
Example: db. foo. ensuIndex ({x: 1, y: 1, z: 1}) queries the indexed fields and only returns these fields. Therefore, mongodb does not need to load the entire document. Db, foo. find ({x: ceriteria, y: ceriteria })
This type of query only accesses the index data, but does not access the data of the entire set. Tip 5: Use a composite index to accelerate multiple query queries. You only need to match the first part of the index to use the index, therefore, when creating an index, consider all the fields that these queries depend on. Define the order of index fields based on the frequency of query for each field. Tip 6: organize arrays in layers by establishing a hierarchical document to accelerate scanning, which not only makes them look more organized, it also allows mongodb to quickly query data without indexing. If the document does not have a hierarchical structure, mongodb must traverse each field in the document. Reasonable Use of layers can reduce mongodb's access to fields.
Tip 7: The AND-type query key points assume that you want to query documents that meet the conditions A, B, and c. There are 40 thousand documents that meet the requirements of A, 9 K for B, AND 200 for C, C and B and A should be used to query only 200 records.
That is to say, if a query condition is known to be more demanding, it should be placed at the beginning. Tip 8: The OR type query points are the opposite of the AND query, the most matched query statements are placed at the beginning, because Mongodb needs to match documents that are not in the result set each time.







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.