Why do mongoDB add index in big data environment

  • 2021-01-02 22:02:37
  • OfStack


Indexes can often greatly improve the efficiency of queries. When using queries in your system, you should consider creating relevant indexes.

When mongodb is storing big data, it needs to add index to the field of query. What I tested is the data volume of more than 300,000 in Aliyun. The query without index has reached 8 seconds, but after adding index, it is millisecond!

Index the collection

mongodb supports adding indexes to embedded properties

db.agencyTotal.createIndex({"occurDate ":1});

Add only 1 index

db.agencyTotal.createIndex({"code ":1},{"unique":1});
Parameters for createIndex to index background Boolean index building blocks other database operations and background can specify a later stage to create the index by adding the "background" optional parameter. The default value for "background" is false. Whether unique Boolean has only 1 index. Specify that only 1 index be created for true. The default is false. Name of the name string index. If not specified, MongoDB generates 1 index name by the field name and sort order of the concatenated index. sparse Boolean does not enable indexes for field data that does not exist in the document; This parameter requires special attention because if set to true, documents that do not contain corresponding fields will not be queried in the index field. The default is false. expireAfterSeconds integer specifies 1 value in seconds, completes the TTL setting, and sets the survival time of the set. Version number of v index version index. The default index version depends on the version that runs when mongod creates the index. The index weight value of weights document, between 1 and 99,999, represents the score weight of the index relative to other index fields. For a text index, this parameter determines the list of rules for stopping words and for word stems and word makers. English by default language_override string For text indexes, this parameter specifies the name of the field to be included in the document, and the language overrides the default language with the default value language. View index


View index size


Remove the index

db.col.dropIndex(" The index name ")


Related articles: