mongodb index

The principle of indexing

When multiple documents are inserted into a certain collection, each document will have a location information after being persisted by the underlying storage engine. Through this location information, the document can be read from the storage engine.

MMAPv1: file id + offset within the file

WiredTiger: A key generated by WiredTiger when storing a document, through which the corresponding document can be accessed.

CPU soaring? Still no results after half a day? To increase the speed, have you remembered the index! !


After the index is established, MongoDB will additionally store an index data sorted in ascending order by the age field. The index structure is similar to the following. The index usually adopts a btree-like structure for persistent storage to ensure that the corresponding position of an age value can be quickly found from the index. information, and then the corresponding document can be read according to the location information.

image.png

Indexing is to organize documents in order according to a certain field (or some) so that they can be efficiently queried according to this field. The basic role of the index: search + sort


With indexes, at least the efficiency of the following scenarios can be optimized:

    Query, such as querying everyone whose age is 18.

    Update/Delete, update or delete the information of all people aged 18, because when updating or deleting, all documents that meet the conditions need to be queried first, so the query is still optimized in essence.

    Sort, sort all the information by age, if there is no index, you need to scan the document in the full table, and then sort the results of the scan.

    When indexes exist, performing DML operations will be slower (maintaining indexes).

    By default, MongoDB will generate the _id field for the inserted document (if the application itself does not specify this field), and _id is the unique identifier of the document. In order to ensure that the document can be queried according to the document id, MongoDB will create an index of the _id field for the collection by default.


Index Features

Indexes are stored in memory (RAM), and it should be ensured that the size of the index does not exceed the memory limit.

If the size of the index is larger than the memory limit, MongoDB will drop some indexes, which will lead to performance degradation.


Index scan type:

Index coverage: If all the required fields are in the index, and no additional fields are required, the requirements of index coverage can be satisfied, and data is no longer required to be loaded from the data page, which is index coverage.

index scan

Collection scan


Index restrictions

1. Each collection is limited to 64 indexes.

2. The length of the index name cannot exceed 125 characters.

3. A composite index can have up to 31 fields.

4. All index fields are an array, and the index cannot be used to cover the query.

5. Regular expressions and non-operators, such as $nin, $not, etc.; arithmetic operators, such as $mod, etc.; $where clause.

6. The more indexes, the worse the write performance. For example, for a collection that is frequently modified, its index reaches 20-30, which is a serious bottleneck in index performance.

7. Indexing is an IO-intensive operation, especially when your collection is large. This is the case with all database systems that support secondary indexes, including MySQL. If you need to build an index on a large collection, consider building it in the background.

8. If the collection is rarely read, you can not use the index

type of index

1. Single field index

2. Composite index

3. Multi-key index

4. Hash index

5. Geographic Bitmap Index

6. TTL index

7. Full-text index



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324631293&siteId=291194637