当前位置:网站首页>[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
边栏推荐
- How to install and configure altaro VM backup for VMware vSphere
- @Solutions to null pointer error caused by Autowired
- 今天很多 CTO 都是被干掉的,因为他没有成就业务
- 【一起上水硕系列】Day 7 内容+Day8
- PHP笔记超详细!!!
- 卷积神经网络CNN中的卷积操作详解
- [untitled]
- Altaro VM backup getting started
- [together Shangshui Shuo series] day 7 content +day8
- ROS Compilation Principle
猜你喜欢

Win10 install pytullet and test

Apt update and apt upgrade commands - what is the difference?

【一起上水硕系列】Day 7 内容+Day8

Why should we rewrite hashcode when we rewrite the equals method?
![[set theory] relational closure (relational closure related theorem)](/img/6a/b6dca7abf592f8d8ab1d6aecc43381.jpg)
[set theory] relational closure (relational closure related theorem)
![[set theory] relational closure (reflexive closure | symmetric closure | transitive closure)](/img/c8/2995c503e9dabae4e2cc704449e04f.jpg)
[set theory] relational closure (reflexive closure | symmetric closure | transitive closure)

Progressive multi grasp detection using grasp path for rgbd images

大二困局(复盘)

Configure DTD of XML file

Mapbox tasting value cloud animation
随机推荐
Redhat7 system root user password cracking
期末复习(day3)
Can altaro back up Microsoft teams?
Brief introduction of realsense d435i imaging principle
Strategy pattern: encapsulate changes and respond flexibly to changes in requirements
redis 无法远程连接问题。
期末复习(DAY7)
Disassembly and installation of Lenovo r7000 graphics card
Redis cannot connect remotely.
Jetson AGX Orin 平台移植ar0233-gw5200-max9295相机驱动
Mapbox tasting value cloud animation
Final review (day3)
Xaml gradient issue in uwp for some devices
College campus IP network broadcasting - manufacturer's design guide for college campus IP broadcasting scheme based on campus LAN
Redis encountered noauth authentication required
redis 遇到 NOAUTH Authentication required
mysql启动报错:The server quit without updating PID file几种解决办法
Method of finding prime number
Source insight operation manual installation trial
Niuke JS separator
