当前位置:网站首页>[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
边栏推荐
- Brief introduction of realsense d435i imaging principle
- Robot capture experiment demonstration video
- The request database reported an error: "could not extract resultset; SQL [n/a]; needed exception is org.hibernate.exception.sqlgram"
- 一起上水碩系列】Day 9
- [untitled]
- 32GB Jetson Orin SOM 不能刷机问题排查
- [together Shangshui Shuo series] day 7 content +day8
- Final review (Day7)
- 卷积神经网络CNN中的卷积操作详解
- Export the altaro event log to a text file
猜你喜欢

PHP notes are super detailed!!!

Method of finding prime number
![[branch and cycle] | | super long detailed explanation + code analysis + a trick game](/img/aa/543d4f0dcbcd664be963579af77ec9.jpg)
[branch and cycle] | | super long detailed explanation + code analysis + a trick game

Redis cannot connect remotely.

Configure DTD of XML file

"C and pointer" - Chapter 13 function pointer 1: callback function 2 (combined with template to simplify code)

2022.DAY592

@Solutions to null pointer error caused by Autowired

QT read write excel -- qxlsx insert chart 5

Shanghai daoning, together with American /n software, will provide you with more powerful Internet enterprise communication and security component services
随机推荐
Redhat7 system root user password cracking
Map的扩容机制
How does win7 solve the problem that telnet is not an internal or external command
MySQL startup error: several solutions to the server quit without updating PID file
为什么网站打开速度慢?
chromedriver对应版本下载
[set theory] relational closure (relational closure related theorem)
Primary school campus IP network broadcasting - Design of primary school IP digital broadcasting system based on campus LAN
[set theory] relational closure (reflexive closure | symmetric closure | transitive closure)
[trivia of two-dimensional array application] | [simple version] [detailed steps + code]
How to create your own repository for software packages on Debian
期末复习DAY8
JS implements the problem of closing the current child window and refreshing the parent window
Redis encountered noauth authentication required
[advanced pointer (1)] | detailed explanation of character pointer, pointer array, array pointer
CAD插件的安装和自动加载dll、arx
"C and pointer" - Chapter 13 function of function pointer 1 - callback function 1
獲取並監控遠程服務器日志
Azure file synchronization of altaro: the end of traditional file servers?
Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
