Home > Database > Mysql Tutorial > Summary of MongoDB methods to improve performance

Summary of MongoDB methods to improve performance

不言
Release: 2018-09-19 15:21:27
Original
2077 people have browsed it

What this article brings to you is a summary of methods to improve MongoDB performance. It has certain reference value. Friends in need can refer to it. I hope it will be helpful to you.

MongoDB is a high-performance data, but in the process of using it, everyone occasionally encounters some performance problems. MongoDB compared to other relational databases, such as SQL Server, MySQL, Oracle In comparison, it is relatively new and many people are not very familiar with it, so many developers and DBAs tend to focus on the realization of functions and ignore the performance requirements. In fact, MongoDB and SQL Like Server, MySQL, and Oracle, the design adjustment of a database object, the creation of indexes, and the optimization of statements will have a huge impact on performance.

In order to fully tap the performance of MongoDB, we have simply summarized the following 18 items. Everyone is welcome to continue to summarize and improve them.

(1) It is recommended to use the default value for the _id key in the document, and it is prohibited to save customized values ​​to _id.

Interpretation: There will be a "_id" key in the MongoDB document, which defaults to an ObjectID object (the identifier includes timestamp, machine ID, process ID and counter). MongoDB has a big difference in insertion speed when specifying _id and not specifying _id. Specifying _id will slow down the insertion rate.

(2) It is recommended to use short field names.

Interpretation: Unlike relational databases, each document in the MongoDB collection needs to store a field name, and long field names will require more storage space.

(3) MongoDB index can improve document query, update, delete, and sort operations, so create an index appropriately based on business needs.

(4) Each index will occupy some space and cause resource consumption for the insertion operation. Therefore, it is recommended that the number of indexes in each collection be controlled within 5 as much as possible.

(5) For queries that contain multiple keys, creating a composite index containing these keys is a good solution. The key value order of a composite index is very important. Understand the leftmost prefix principle of the index.

Interpretation: For example, create a combined index {a:1,b:1,c:1} on the test collection. Execute the following 7 query statements:

db.test.find({a:”hello”}) // 1
db.test.find({b:”sogo”, a:”hello”}) // 2
db.test.find({a:”hello”,b:”sogo”, c:”666”}) // 3
db.test.find({c:”666”, a:”hello”}) // 4
db.test.find({b:”sogo”, c:”666”}) // 5
db.test.find({b:”sogo” }) // 6
db.test.find({c:”666”}) // 7
Copy after login

The above query statements may be indexed 1, 2, 3, 4

The query should include the leftmost index field, and the index creation order shall prevail. The order of query fields is irrelevant.

The least index covers the most queries.

(6) TTL index (time-to-live index, index with life cycle), using TTL index can age documents within the timeout period, and a document will be deleted after reaching the aging level.

Interpretation: The index used to create TTL must be of date type. TTL index is a single field index and cannot be a composite index. TTL delete document background thread removes invalid documents every 60 seconds. Fixed-length collections are not supported.

(7) When you need to create an index on a certain field in the collection, but a large number of documents in the collection do not contain this key value, it is recommended to create a sparse index.

Interpretation: The index is intensive by default, which means that even if the index field of the document is missing, there is a corresponding relationship in the index. In a sparse index, only documents containing index key values ​​will appear.

(8) When creating a text index, the field specifies text instead of 1 or -1. There is only one text index per collection, but it can index as many fields as you like.

Interpretation: Text search is much faster. It is recommended to use text index to replace inefficient queries on multiple fields of collection documents.

(9) Use findOne to query matching multiple items in the database, and it will return the first item in the naturally sorted file collection. If you need to return multiple documents, use the find method.

(10) If the query does not need to return the entire document or is only used to determine whether a key value exists, you can limit the returned fields through projection (mapping) to reduce network traffic and client memory usage.

Interpretation: You can either explicitly specify the returned fields by setting {key:1}, or you can set {key:0} to specify the fields that need to be excluded.

(11) Except for prefix style queries, regular expression queries cannot use indexes and take longer to execute than most selectors. They should be used sparingly.

(12) In the aggregation operation, $ must be before match and $group. By prefixing $, you can reduce the prefix of match and reduce the number of documents to be processed by the $group operator.

(13) Modifying documents through operators can usually achieve better performance, because there is no need to go back and forth to the server to obtain and modify document data, and less time can be spent on serializing and transmitting data. time.

(14) Batch Insert (batchInsert) can reduce the number of data submissions to the server and improve performance. However, the BSON Size submitted in batches does not exceed 48MB.

(15) It is forbidden to retrieve too much data for sorting at one time. MongoDB currently supports sorting result sets within 32M. If sorting is required, try to limit the amount of data in the result set.

(16) Some $ operators in the query may cause low performance, such as $ne, $, not, $exists, $nin, $or try to use Do not use in business.

a) $exist: Because of the loose document structure, the query must traverse each document;

b) $ne: If the negated value is the majority, the entire index will be scanned ;

c) $not: may cause the query optimizer to not know which index should be used, so it will often degrade to a full table scan;

d) $nin: a full table scan;

e) \$If there are multiple conditions, it will be queried as many times as possible. When merging the result set, you should consider changing it to or: If there are multiple conditions, you will be querying how many times. When merging the result set, you should consider changing it to $in.

(17) Fixed collections can be used to record logs, which insert data faster and can eliminate the oldest data when inserting data. This feature can be considered during demand analysis and design, which improves performance and eliminates the need for deletion.

Interpretation: Fixed collections need to be created explicitly, specify the Size, and can also specify the number of documents. No matter which limit is reached first, new documents inserted later will remove the oldest document.

(18) The data volume of documents in the collection will affect query performance. To maintain an appropriate volume, regular archiving is required.

The above is the detailed content of Summary of MongoDB methods to improve performance. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template