当前位置:网站首页>[teacher Zhao Yuqiang] index in mongodb (Part 2)
[teacher Zhao Yuqiang] index in mongodb (Part 2)
2022-07-03 05:46:00 【Teacher zhaoyuqiang】
( Four ) Index type 3 : Composite index (Compound Index)
MongoDB Support composite index , To combine multiple keys to create an index . This approach is called composite index , Or composite index , This method can satisfy the situation of using index in multi key value matching query . Secondly, when using composite index , Index can also be used by prefix method .MongoDB The composite index in is basically consistent with the relational database . Some of the principles of composite indexing in relational databases also apply to MongoDB.
In the previous content , We've been emp A composite index is created on the collection , as follows :
db.emp.createIndex({"deptno":1,"sal":-1})
The following uses different filter conditions to query documents , Check the corresponding execution plan :
(1) Use only deptno As a filter
db.emp.find({"deptno":10}).explain()
(2) Use deptno、sal As a filter
db.emp.find({"deptno":10,"sal":3000}).explain()
(3) Use deptno、sal As a filter , But put sal On the front
db.emp.find({"sal":3000,"deptno":10}).explain()
(4) Use only sal As a filter
db.emp.find({"sal":3000}).explain()
( 5、 ... and ) Composite index and sort
Composite indexes are created in ascending or descending order to specify how they are arranged . For single key indexes , The order is not particularly important , because MongoDB You can traverse the index in any direction . For composite indexes , How to sort can determine whether the index can be used in the query .
db.emp.createIndex({"deptno":1,"sal":-1})
In the previous content , We've been deptno In ascending order 、sal A composite index is created in descending order on the , Next, test the order of the next , Whether the index has been executed :
When the index is used :
db.emp.find().sort({"deptno":1,"sal":-1}).explain()
db.emp.find().sort({"deptno":-1,"sal":1}).explain()
Without using the index :
db.emp.find().sort({"deptno":1,"sal":1}).explain()
db.emp.find().sort({"deptno":-1,"sal":-1}).explain()
Swap the positions of two columns , Retest .
( 6、 ... and ) Composite index and index prefix
The index prefix refers to a subset of a composite index , If there is an index like this :
db.emp.createIndex({"deptno":1,"sal":-1,"job":1})
Then there is the following index prefix :
{"deptno":1}
{"deptno":1,"sal":-1}
stay MongoDB in , In the case of the following query filter conditions , The index will be used to :
db.emp.find().sort({deptno:1,sal:-1,job:1}).explain()
db.emp.find().sort({deptno:1,sal:-1}).explain()
db.emp.find().sort({deptno:1}).explain()
In the case of the following query filter conditions , The index will not be used to :
db.emp.find().sort({deptno:1,job:1}).explain()
db.emp.find().sort({sal:-1,job:1}).explain()
( 7、 ... and ) Summary
- Composite indexes are based on multiple keys ( Column ) Index created on
- Composite indexes can be created for each key ( Column ) To specify the sort method
- The sorting method of the key column affects the operation of the query when sorting , Only those in the same or opposite direction can be matched
- Composite index and prefix index can only be used if they match
边栏推荐
- JS implements the problem of closing the current child window and refreshing the parent window
- Gan network thought
- 中职网络子网划分例题解析
- Source insight operation manual installation trial
- mapbox尝鲜值之云图动画
- 一起上水硕系列】Day 9
- 70 shell script interview questions and answers
- 6.23 warehouse operation on Thursday
- 今天很多 CTO 都是被幹掉的,因為他沒有成就業務
- Solve the problem of automatic disconnection of SecureCRT timeout connection
猜你喜欢
期末复习(Day5)
Brief introduction of realsense d435i imaging principle
Win10 install pytullet and test
Map的扩容机制
Exception when introducing redistemplate: noclassdeffounderror: com/fasterxml/jackson/core/jsonprocessingexception
Classification and discussion of plane grab detection methods based on learning
一起上水碩系列】Day 9
How to use source insight
Solve the problem of automatic disconnection of SecureCRT timeout connection
"C and pointer" - Chapter 13 function pointer 1: callback function 2 (combined with template to simplify code)
随机推荐
[Shangshui Shuo series together] day 10
Method of finding prime number
Together, Shangshui Shuo series] day 9
70 shell script interview questions and answers
配置xml文件的dtd
How do I migrate my altaro VM backup configuration to another machine?
[untitled]
Redhat7 system root user password cracking
Get and monitor remote server logs
"C and pointer" - Chapter 13 advanced pointer int * (* (* (*f) () [6]) ()
2022.6.30DAY591
Use telnet to check whether the port corresponding to the IP is open
How does win7 solve the problem that telnet is not an internal or external command
Capacity expansion mechanism of map
大二困局(复盘)
Niuke JS separator
Explanation of several points needing attention in final (tested by the author)
2022.6.30DAY591
Altaro o365 total backup subscription plan
Calculation method of AUC