MongDB optimization

MongDB optimization

MongoDB is a high-performance data, but the use of the process, we occasionally run into some performance issues. MongoDB and other relational databases compared to, for example SQL Server, MySQL, Oracle contrast, is relatively new, many people are not familiar with them, so many developers, DBA often focus on achieving functions at the expense of performance Claim. In fact, MongoDB and SQL Server, MySQL, Oracle, like, adjust the design to create a database objects, index, optimize statement, will have a huge impact on performance.
In order to fully tap the MongoDB performance, simple Mongodb include the following optimizations:

  1. Document _id key is recommended to use the default values, not store custom values to _id in.
    Interpretation: MongoDB document will have a "_id" key, the default is ObjectID objects (identifier includes a time stamp, the machine ID, process ID and counter). MongoDB specified _id when you do not specify _id insertion speed vary greatly, will slow down the rate specified _id inserted.
  2. Recommended for short field names.
    Interpretation: with relational databases, MongoDB collection need to store each document field names, field names longer will need more storage space.
  3. MongoDB document index can improve query, update, delete, sort operations, so the combination of business needs, create an appropriate index.
  4. Each index will take up some space, resource consumption and results in the insertion operation, therefore, recommended that each index number is set at less than 5 try to control.
  5. For queries that contain a plurality of keys, create a composite index that contains these keys is a good solution. Key sequence composite index is important to understand the principle of the most left-prefix index.
    Interpretation: In the test set, for example, to create a composite index {a: 1, b: 1 , c: 1}. Perform the following seven query:
  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

Above query might take the index is 1,2,3,4
query should contain the most left-field index, the index creation order to prevail, regardless of the query field order.
Minimum index covers most queries.
6. TTL index (time-to-live index, with a life cycle of the index), you can use TTL index aging time of the document, a document will be deleted after reaching the degree of aging.
Interpretation: TTL create an index must be a date type. TTL field index index is a single, composite index can not be. TTL delete documents every 60s background thread to remove invalid documents. It does not support fixed-length collection.
7. The need to create an index on a field collection, but the collection does not contain a lot of this key document, it is recommended to create a sparse index.
Interpretation: The default index is intensive, which means that even if a document index fields missing in the index there is also a corresponding relationship. In the sparse index, the only index contains the key documents appear.
8. When creating text field specifies text index, rather than 1 or -1. Each set is only a text index, but it can index any number of fields.
Interpretation: Text search is much faster, it is recommended to use alternative text index inefficient queries on multiple-field collection of documents.
9. Use findOne multiple items that match the query in the database, it will return the first project in the natural order file collection. If you need to return multiple documents, use the find method.
10. If the query without having to return the entire document or just a key value used to determine whether there is, return to a field may be limited to reduce the memory usage and network traffic clients through the projection (mapping).
Interpretation: either {key: 1} by setting the field to explicitly specify the return may be provided {key: 0} specified field to exclude
11. prefix style query, the query can not use the regular expression index, in addition to execution time longer than most selector, of restraint should use them.
12. Bulk insert (batchInsert) submit data to the server can reduce the number of times, to improve performance. But BSON Size batch submitted does not exceed 48MB
13. The ban takes out too much data to sort, MongoDB currently supports 32M within the result set to be sorted. If you need to order, please try to limit the amount of data in the result set.

Published 111 original articles · won praise 31 · views 110 000 +

Guess you like

Origin blog.csdn.net/weixin_45678915/article/details/104812937