当前位置:网站首页>[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
边栏推荐
- 2022.7.2day594
- 求质数的方法
- Download the corresponding version of chromedriver
- Final review (day3)
- Primary school campus IP network broadcasting - Design of primary school IP digital broadcasting system based on campus LAN
- Win10 install pytullet and test
- 3dslam with 16 line lidar and octomap
- Analysis of the example of network subnet division in secondary vocational school
- Jetson AGX Orin 平台移植ar0233-gw5200-max9295相机驱动
- 期末复习(Day5)
猜你喜欢

Communication - how to be a good listener?

6.23 warehouse operation on Thursday

Method of finding prime number

【一起上水硕系列】Day 10

Map的扩容机制

Error 1045 (28000) occurs when Linux logs in MySQL: access denied for user 'root' @ 'localhost' (using password: yes)

Linux登录MySQL出现ERROR 1045 (28000): Access denied for user ‘root‘@‘localhost‘ (using password: YES)

配置xml文件的dtd

今天很多 CTO 都是被干掉的,因为他没有成就业务

Sophomore dilemma (resumption)
随机推荐
kubernetes资源对象介绍及常用命令(五)-(ConfigMap)
2022.7.2day594
Final review (Day2)
Together, Shangshui Shuo series] day 9
Beaucoup de CTO ont été tués aujourd'hui parce qu'il n'a pas fait d'affaires
mysql启动报错:The server quit without updating PID file几种解决办法
Redis cannot connect remotely.
Installing altaro VM backup
Xaml gradient issue in uwp for some devices
卷积神经网络CNN中的卷积操作详解
MySQL startup error: several solutions to the server quit without updating PID file
32GB Jetson Orin SOM 不能刷机问题排查
Redhat7 system root user password cracking
Final review (day3)
Method of finding prime number
期末复习(DAY6)
"C and pointer" - Chapter 13 function of function pointer 1 - callback function 1
[branch and cycle] | | super long detailed explanation + code analysis + a trick game
一起上水碩系列】Day 9
[advanced pointer (2)] | [function pointer, function pointer array, callback function] key analysis + code explanation
