mongoDB index summary

introduce

The storage engine used by MongoDB is WiredTiger, and the index construction uses B+ tree.

index type

single index

db.members.createIndex({name:1});

composite index

db.members.createIndex({ name:1,age:-1});

other indexes

Geospatial Index, Text Indexes, Hashed Indexes.

#spatial index
 

db.members.createIndex({location : "2dsphere"})

#text index

db.reviews.createIndex( { comments: "text" } )

# hash index

db.collection.createIndex( { _id: "hashed" } )

Source: Geospatial Index of Index Types

index management

Check

View all indexes
#This syntax command requires MongoDB 3.0+

db.collection.getIndexes()

create

grammar:

db.collection.createIndex(keys, options)

Key: The value is the index field you want to create

1: Specify to create indexes in ascending order
-1: Create indexes in descending order


example

db.members.createIndex({name:1});

options: receive optional parameters

 example

#background

db.values.createIndex({open: 1, close: 1}, {background: true})

# create a unique index

If a field in a document has duplicate values, it is not possible to create a unique index for that field.
If the newly added document does not have a uniquely indexed field, only the first document missing the field can be added, and the key value in the index is set to null.

db.members.createIndex({age:1},{unique:true});

# index sparsity

If the same index is both unique and sparse, multiple documents with missing index key values ​​can be saved.

db.sparsedemo.createIndex({name:1},{unique:true ,sparse:true});

Source: MongoDB Create Index_Shuiyue Qinghui's Blog-CSDN Blog_mongodb Create Index

delete 

#Delete by index name

db.members.dropIndex("索引名称")

#Delete by definition

db.members.dropIndex({name:1})

Execution plan analysis (explain)

Analyze query performance

Analyze query performance (Analyze Query Performance) usually uses the execution plan (explain plan, Explain Plan) to view the query situation, such as the time spent by the query, whether it is based on index query, etc.
 

 db.members.explain().find({name:"xxx"})

Here we need to focus on the value meaning of stage in winningPlan:

COLLSCAN: scan the entire collection
IXScan: index scan
FETCH: query according to the address of the document pointed to by the index (equivalent to the query back to the table in mysql)
PROJECTION_COVERED: map coverage, no need to go back to the table query
SORT: need to sort in memory, the efficiency is not good high

coverage query

When the query criteria and the query's <projection> contain only indexed fields, MongoDB returns results directly from the index without scanning any documents or bringing documents into memory. These covered queries can be very efficient.

db.members.explain().find({ name:"zhangsan"},{_id:0, name:1});

In actual use, we should try to optimize winningPlan.stage to PROJECTION_COVERED.

data lifetime

For date fields, or array fields containing date elements, you can use an index with a time-to-live to automatically delete documents whose field values ​​exceed the time-to-live.

structure data

db.members.insertMany( [ 
    {
     name:"zhangsanss",
     age:19,tags:["00","It","SH"],
     create_time:new Date()}
    ] );

construct index

db.members.createIndex({ create_time: 1},{expireAfterSeconds:30 });

A data with a survival time of 30s is created on the create_time field.

Composite key indexes do not have time-to-live properties

When the index key is an array field containing date elements, the smallest date in the array will be used to calculate whether the document has expired.

The database uses a background thread to monitor and delete expired documents, and the delete operation may have a certain delay.

source:

Detailed Explanation of MongoDB Index

Guess you like

Origin blog.csdn.net/csdncjh/article/details/128409977