当前位置:网站首页>[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
边栏推荐
- "C and pointer" - Chapter 13 function pointer 1: callback function 2 (combined with template to simplify code)
- mapbox尝鲜值之云图动画
- Making coco datasets
- 【无标题】
- QT read write excel -- qxlsx insert chart 5
- Calculation method of AUC
- Final review Day8
- Btrfs and ext4 - features, strengths and weaknesses
- Use telnet to check whether the port corresponding to the IP is open
- PHP笔记超详细!!!
猜你喜欢
mapbox尝鲜值之云图动画
Altaro virtual machine replication failed: "unsupported file type vmgs"
Training method of grasping angle in grasping detection
Brief introduction of realsense d435i imaging principle
【一起上水硕系列】Day 7 内容+Day8
Life is a process of continuous learning
How does win7 solve the problem that telnet is not an internal or external command
Kubernetes resource object introduction and common commands (V) - (configmap)
How to set up altaro offsite server for replication
"C and pointer" - Chapter 13 function of function pointer 1 - callback function 1
随机推荐
Making coco datasets
2022.7.2 simulation match
SimpleITK学习笔记
@Autowired 导致空指针报错 解决方式
为什么网站打开速度慢?
Altaro set grandfather parent child (GFS) archiving
Training method of grasping angle in grasping detection
Calculation method of AUC
Installing altaro VM backup
2022.6.30DAY591
期末复习(Day5)
ninja: build stopped: subcommand failed.
Ext4 vs XFS -- which file system should you use
Win10 install pytullet and test
Disassembly and installation of Lenovo r7000 graphics card
獲取並監控遠程服務器日志
[branch and cycle] | | super long detailed explanation + code analysis + a trick game
C 语言文件操作函数大全 (超详细)
[set theory] relational closure (relational closure related theorem)
How to set up altaro offsite server for replication