This article is a mirror article of machine translation, please click here to jump to the original article.

View: 5609|Reply: 2

MongoDB Viewing and Creating Indexes

[Copy link]
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:

MongoDB common query commands
https://www.itsvse.com/thread-10169-1-1.html

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.

parametermeaning
totalKeysExaminedNumber of index scans
totalDocsExaminedNumber of document scans
nReturnedThe number of results returned
executionTimeMillisExecution is time-consuming
executionSuccessWhether 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
 Landlord| Posted on 2/11/2022 5:49:16 PM |
Slow query analysis process:

1. Use the slow query log (system.profile) to find statements that exceed 200ms

2. Then use .explain() to analyze the number of affected rows and analyze why it exceeds 200ms

3. Decide if you need to add an index

Enable slow query:


Parameter:

0: Closed, no data is collected.
1: Collect slow query data, the default is 100 milliseconds.
2: Collect all data

Review the results:


Note: Indexes can basically be said to be permanent in memory, if the amount of data reaches 100 million, the smaller the number of indexes, the better, because it will occupy a large amount of memory. According to the Internet, 1 million indexes account for about 50M memory. If it is 100 million, then it will occupy 5G memory.

Posted on 2/13/2022 4:30:52 PM |
learned...
Disclaimer:
All software, programming materials or articles published by Code Farmer Network are only for learning and research purposes; The above content shall not be used for commercial or illegal purposes, otherwise, users shall bear all consequences. The information on this site comes from the Internet, and copyright disputes have nothing to do with this site. You must completely delete the above content from your computer within 24 hours of downloading. If you like the program, please support genuine software, purchase registration, and get better genuine services. If there is any infringement, please contact us by email.

Mail To:help@itsvse.com