MongoDB study notes (five, MongoDB storage engine and index)

table of Contents:

  • mongoDB storage engine
  • mongoDB Index
  • The property index
  • MongoDB query optimization

mongoDB storage engine:

MongoDB currently divided into three types of storage engines:

1, WiredTiger storage engine :

a, Concurrency (Concurrent level) : WiredTiger support document-level concurrency, support multiple clients simultaneously modify a document.

b, Snapshots and Checkpoints (snapshots and checkpoints) : WiredTiger create every 60s a checkpoint (snapshot data is written to disk), between this mongo or server downtime will lose data.

c, Journal (intermediate checkpoint log) : for the optimization of Checkpoint, at a checkpoint persisted to disk before the data will first deposit to the Journal .

If the log space is smaller than 128B is not compressed , high compression algorithm is enabled (using snappy algorithm compressed data). Set compression algorithm: storage.wiredTiger.engineConfig.journalCompressor

d, Compression (compression algorithm) : The consumption of CPU resources to reduce the consumption of memory space . Default block compression algorithm compressed set of data, Snappy algorithm (prefix) Compress index .

Setting the set compression algorithms: storage.wiredTiger.collectionConfig.blockCompressor.

Set Index compression algorithms: storage.wiredTiger.indexConfig.prefixCompression.

e, Memory Use (memory usage) :

There are two memory, (RAM - 1GB) of the mongo WiredTiger engine * 0.5,256M; default therebetween which a large memory as mongo use.

Set MongoDB available memory size of the unit GB: storage.wiredTiger.engineConfig.cacheSizeGB

 

2, MMAPv1 storage engine :

a、Journal:同WiredTiger。

b, Record Storage (data storage): continuous data stored on disk, when a document needs more space will involve data movement, and also to update the index, but also lead to disk fragmentation.

c, Memory Use: use all the memory space

 

3, InMemory storage engine :

a、Concurrency:同WiredTiger。

b、Memory Use:RAM * 0.5 - 1GB

mongoDB Index:

mongoDB index is divided into four kinds:

1, the key index: in a particular attribute indexing on.

Syntax: db.collectionName.createIndex ({ 'name': - 1}) -1 = descending, ascending 1 =

mongoDB ID on the establishment of a unique key index, an exact match on the field, sort and find the range will use this index.

 

2, composite index: a plurality of specific properties of indexing on.

语法:db.collectionName.createIndex({'name':-1, age:1})

An exact match on the field, sort and find the range will use this index, but the index of the relevant order.

For performance reasons, you should delete the first one there is the same key bond indexes.

Such as: the need to find a query based on name and age

If only the index name, the same name that the situation will lead to more efficient queries reduce; you might say, to build a age of le index does not have to, of course not, because mongo a query can only use one index, so like this situation needs to establish a composite index to improve query efficiency.

To delete that why there is identical to the first key-key index it, it comes on top of a query can only use one index, and the composite index may use a prefix query instead of a single name index, so the index is a single name a waste.

 

3, multi-key indexing: indexing the array.

语法:db.collectionName.createIndex({'address.city':1})

 

4, hash indexes:

语法:db.collectionName.createIndex({'name':'hashed'})

Hash index on the inlet are uniformly distributed, useful slice set.

Index attributes:

Create an index:

db.collectionName.createIndex(
    {'name':1},
    {
        'background':true,
        'unique':true,
    'name':'indexName',
    'sparse':true
    }
)

Delete the index:

1, delete the specified name: db.collectionName.dropIndex ( 'indexName')

2, delete indexes on the set (_id cut can not): db.collectionName.dropIndexs ()

3, rebuild the index on the set: db.collectionName.reIndex ()

4, the index set of queries: db.collectionName.getIndexs ()

MongoDB query optimization:

1, slow queries positioning

Open slow query: db.setProfilingLevel (n, {m})

n has three possible values:

  • 0; not recorded (the default value).
  • 1; record slow query log, it must be specified as 1 m, that is, the query time threshold, unit ms.
  • 2; log all slow query log.

After the slow query MongoDB open, the log will be stored in system.profile of the collection, its maximum allocation 128K space.

May be used to sort $ nartual slow query log, such as: db.system.profile.find () Sort ({ 'Natural $': -. 1}).. Limit (. 5)

 

2, slow query analysis

Can be analyzed by the query plan explain slow query, such as db.collectionName.find ({ 'age': { '$ lt': 50}}). Explain ( 'executionStats')

explain optional parameters:

  • queryPlanner; the default, meaning only show the execution plan information.

  • executionStats; represents information showing the execution plan to showcase the implementation of information selected execution plan.

  • allPlansExecution; represents information showing the execution plan, and present information on the implementation of the selected execution plan, the implementation of information also shows an alternative execution plan.

 

3, using the index on recommendations

According to demand the establishment of the index, it will be useful but there are costs, not to those who write once read many less indexing .

Try to ensure that each stage of the query are IXSCAN , the pursuit of the number of scanning a document (totalDocsExamined) = Returns the number of (nReturned) document .

MongoDB in a query with only an index, if the multi-criteria query to make use of the composite index .

Indexing the amount of data is very time consuming and more resources, so try to put a small amount of data indexing built in the time .

Guess you like

Origin www.cnblogs.com/bzfsdr/p/11973518.html