In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-13 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >
Share
Shulou(Shulou.com)06/01 Report--
1. Introduction to the index:
The index of monggodb is also a balanced binary tree, so most of the index optimization techniques in traditional databases are available.
Note: mongodb can traverse data in any direction (unlike indexes in relational databases), but this is limited to single-key sorting, which is important for the direction of multi-key sorting indexes.
The following is a specific example of a single-key ordering that is not directional:
The above is the execution plan of the two queries. The parameters in the red part indicate whether the query has a sort operation in memory.
From the above example, it is obvious that no query has a sort operation in memory, whether it is sorted in descending or ascending order of iage.
There are many index types in 2.mongodb. I only list the more common index types and more index types here. See http://www.cnblogs.com/xinghebuluo/archive/2011/12/19/2293043.html
Unique index: it ensures that documents that repeat the index keywords of existing documents will not be inserted
In the above example, a unique index has been successfully created.
Compound unique index: a unique index with a combination of multiple key values.
Composite unique index instance:
Considerations for creating a unique index:
(1) unique indexes and keywords that do not exist:
When a document saved to the collection has no value in the index field, its index field is assigned to null and inserted. That is, you cannot insert multiple documents in a unique index that have no value in an index field. (multiple null values are considered equal)
The following example inserts multiple null values on a unique index field to report an error.
(2)。 Create a unique index on fields that already exist with duplicate values.
If you create a unique index directly, you will definitely get an error.
(3)。 If you must create a unique index on such a field, you can specify a keyword dropDups to force the creation of the index when creating the unique index. However, this method of creating an index deletes the data in the collection, so it is not recommended.
Use dropDups to force the creation of an index sample:
Sparse index: in the collection of mongdo, the key-value pair of each document can be different (the number of columns in rows is different), so in an index, the field may or may not be in a document. When the field is not in a document, the query will query the document without this field by default when it uses the index to get data.
In the query in the above example, the data is obtained by indexing iage, but the documents that do not contain iage fields are also queried.
At this point, documents that do not contain index fields can be filtered out through sparse indexes.
The following is an example of filtering documents that do not contain index fields through sparse indexes:
From the execution plan, we can see that the query obtains the data through sparse index, and the result set of the query contains all iage fields (documents that do not contain iage are filtered out).
3. Index Management:
View the index information under the defined collection: db.collection.getIndexes ()
View all index information under the current schema: db.system.indexes.find ({})
Create an index: db.collection.ensureIndex ({key:1}}, {unique:true,dropDups:true})
Hint ({}): force the use of a key value (index)
You can also index db.collection.ensureindex ({someFiled:1}, {background:1}) in the background.
Advantages: periodically release the lock so that the customer server can write data.
Cons: time-consuming
Index at the front desk (default):
Advantages: relatively less time-consuming
Disadvantages: the customer server cannot write data during index creation
Delete index: db.collection.dropIndexes (), db.collection.dropIndex (index)
Re-index: db.test.reIndex ()
Identification index: db.collection.ensureIndex ({key:1}}, {name: "Index_name"})
(1): view all indexes of the specified collection: db.colleciton.getIndexes ()
(2) View all index information under the current architecture: db.system.indexes.find ({})
(3) create an index: db.collection.ensureIndex ({key:1}, {unique:true,dropDups:true,sparse:true})
The above is the standard syntax for creating an index, and the second curly braces ({unique:true,dropDups:true,sparse:true}) are the index type parameters.
Unique: represents a unique index.
Sparse: represents a sparse index.
DropDups: when there is a duplicate value in the index field, force the duplicate document to be deleted. This parameter will lose data. This parameter is not recommended.
(4) hint ({}): force the use of a key value (index)
In the following example, you can see from the execution plan that the index is enforced.
(5)。 Delete index: db.collection.dropIndexes (), db.collection.dropIndex (index)
Db.collection.dropIndexes (): deletes all indexes below the collection.
There is no index to delete under the collection test. _ id_ is the system's own index and cannot be deleted manually.
Db.collection.dropIndex (index_name): deletes the specified index
(6)。 When the index is inefficient (probably with more fragments), the index needs to be rebuilt. Db.test.reIndex ().
Db.test.reIndex (): rebuild the index
Rebuild a single index or all indexes under the collection.
4. Interpretation of the implementation plan:
Correctly interpreting the execution plan of a query is the first step in optimizing the query, so you must accurately understand the execution plan of the query:
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.