Project scenario: MONGO in the first query is very slow, the back is good. If you do not query for a long time, the next start will be very slow for the first time, so from the link at that time, to troubleshoot the final discovery or MONGO index built problems.
MongoDB often encounters long queries during large-scale data queries, often using a variety of indexes to optimize.
First step, you can use. Explain () to view the query data traces for easy analysis of the query index for each shard.
Db. Collection.find ({}). Explain ()
If you view the index, you can create a new index based on the query criteria.
1. Querying the Index
Db. Collection_name.getindexes ()
2. Create a new index
Db. Collection_name.ensureindex (Keys[options])
This command supports Shard creation index
keys
A list of parameters to be indexed. such as: {KEY:1}
, which key
represents the field name, 1
indicates an ascending sort, or use numeric -1
descending.
options
, an optional parameter that represents the setting for indexing. The optional values are as follows:
background
A Boolean that indexes in the background so that other database activities are not blocked when the index is built. The default value is False.
unique
A Boolean that creates a unique index. The default value is False.
name
A String that specifies the name of the index. If not specified, MongoDB generates an indexed field with the name and sort order concatenation.
dropDups
, Boolean, when a unique index is created, only the first one is retained if duplicate deletions occur for subsequent occurrences of the same index.
sparse
A Boolean that does not enable indexing for field data that does not exist in the document. The default value is False.
v
, index version, and the revision number of the indexes.
weights
, document, index weight value, which is between 1 and 99,999, indicating the score weight of the index relative to the other indexed fields.
3. Re-create the index
Re-create if the index is found to be inappropriate
Db. Collection_name.reindex ()
4. Deleting an index
Db. Collection_name.dropindex ("Index-name")
MongoDB Query optimization