|
|
Posted on 2/11/2022 5:46:29 PM
|
|
|
|

MongoDB index
Without indexes, MongoDB would have to scan every file in the collection and select those records that meet the query criteria when reading data.
This kind of query efficiency of scanning the full set is very low, especially when processing a large amount of data, the query can take tens of seconds or even minutes, which is very fatal to the performance of the website.
Indexes are special data structures stored in a collection of data that can be easily read over an index, and indexes are a structure that sorts the values of one or more columns in a database table.
Review:
Query the execution plan
Example statements:
Without creating an index (winningPlan:COLLSCAN), as shown in the figure below:
explain() function can also receive different parameters, and you can view a more detailed query plan by setting different parameters.
Parameters include:queryPlanner (default)、executionStats、allPlansExecution
Execute explain("executionStats") and you will find that there are more statistics in the execution plan.
| parameter | meaning | | totalKeysExamined | Number of index scans | | totalDocsExamined | Number of document scans | | nReturned | The number of results returned | | executionTimeMillis | Execution is time-consuming | | executionSuccess | Whether the execution was successful |
Create an index
MongoDB uses the createIndex() method to create indexes.
Note that before version 3.0.0, the index creation method was db.collection.ensureIndex(), and later versions used the db.collection.createIndex() method.
Create a single index
Create a composite index
In the syntax, the key value is the index field you want to create, 1 is specified to create an index in ascending order, and if you want to create an index in descending order, you can specify -1.background to specify that indexes are created in the background, that is, add the "background" optional parameter. "background" defaults to false.
Query indexes
The command is as follows:
Delete the index
Delete all indexes
Delete the specified index
(End)
|
Previous:How to restrict Taiwanese users from registering, post information at will, and ask for advice on how to write.Next:[Actual combat]. NET/C# exports the cache using StackExchange.Redis fuzzy query
|