Mongodb Learning (index details)
This blog will show you how to learn about mongodb indexes in detail, including index management and spatial indexes. Indexes are used to increase our query efficiency. Both relational databases and non-relational databases have the index function. You can add an index on a field or key, when querying this field or key, the efficiency will be greatly improved.
First, let's take a look at the situation before creating an index:
As you can see, here I first add 0.2 million pieces of data to the persons collection, and then use:
db.persons.find({id:80000}).explain()
To calculate the time required to query 8th million pieces of data. As you can see, it consumes 101 milliseconds. The speed is acceptable.
Create an index
Next, I created an index for the id key of the persons set and provided a method for us in mongodb:
Db. Set Name. ensureIndex ({key to create an index: 1 or-1 })
Here, 1 indicates creating an index in ascending order, and-1 indicates creating an index in descending order.
After the index is created, the time is changed to almost 0 ms, which greatly improves the query efficiency.
Note: Using indexes can improve our query efficiency, but it will affect our insertion and modification efficiency, because the index will be maintained during insertion and modification, indexes can be used for collections with Fewer updates and queries.
View Indexes
db.persons.getIndexes()
We can see that there are two indexes. One is that the system creates a "_ id" index by default, and the other is the "number_id" created by ourselves ", we can see that the index name here is the same as our key value by default. If I want to create my own index name, for example, I want to create an index named "personName" for the name, you can write as follows:
<喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4NCjxwcmUgY2xhc3M9 "brush: SQL;"> db.persons.ensureIndex({name:1},{name:"personName"})
Now, there are three indexes in our persons set. We can see that the name of the third name index is the "personName" we gave ourselves"
Create a unique index
Db. persons. ensureIndex ({key: 1 or-1}, {unique: true })
Now, we have created a unique index in descending order for the name key, so we are trying to insert a record with name = "aName0 ".
Insert will fail. The system prompts that the name key has created a unique index.
Remove duplicate values
If a duplicate value exists on the key for creating a unique index before creating a unique index. You can use the following code to remove existing duplicate values:
db.persons.ensureIndex({name:-1},{unique:true,dropDups:true})
Index used for query
If several indexes are created in my collection, you can add hint ({index name: 1 or-1}) to find }), to use the specified index.
Note: The specified index must exist in advance. Otherwise, the query fails.
View created Indexes
The system provides a system. indexex set, which stores the indexes created in all sets:
Create an index Asynchronously
When the data volume is large, it takes some time to create an index. During the index creation process, the set is locked and the set is unlocked, during this time, we cannot perform any operations on the set. At this time, we can create an asynchronous index.
Db. persons. ensureIndex ({key: 1 or-1}, {background: true })
Delete Index
Delete a single index
Db. runCommand ({dropIndexes: "Set Name", index: "index key: 1 or-1 "})
Delete all indexes
Db. runCommand ({dropIndexes: "Set Name", index: "index key :"*"})
Now we can learn about indexes.