当前位置:网站首页>[teacher Zhao Yuqiang] index in mongodb (Part 1)
[teacher Zhao Yuqiang] index in mongodb (Part 1)
2022-07-03 05:46:00 【Teacher zhaoyuqiang】
Index is the most effective way to improve query efficiency . Index is a special data structure , The index stores part of the data in a form that is easy to traverse ( Such as : A specific field or set of field values ), The index sorts the stored values according to certain rules , And the index is stored in memory , It's very fast to retrieve data from the index . If there is no index ,MongoDB Every document in the collection must be scanned , This kind of scanning is very inefficient , Especially when the amount of data is large .
One 、 The basics of indexing
Here's a relational database Oracle For example , Introduce the basic principle of index , As shown in the figure below :
It can be seen from the above picture that , The essence of an index is actually a table of contents for a book . When querying data in a table , Query directory first ( Indexes ) Line address in , Then query the data in the table through the row address , To improve query performance .
The figure below illustrates the MongoDB in , How indexes work in queries and sorts ?
Through this example , You can clearly see that the index stores a specific field or a collection of several fields , And sort them according to certain rules . When you create a collection ,MongoDB Automatically in _id Create a unique index on , Because it's unique , So it can prevent repetition _id Values are inserted into the collection . adopt getIndexes You can look it up MongoDB Index information on a set , As shown in the figure below .
When there is no index , By looking at the execution plan , You can see the query process , as follows : Inquire about :10 Department No , Wages less than 3000 Documents .
So how to create a simple index ? Attention from mongoDB 3.0 Start ensureIndex Abandoned , In the future, it's just db.collection.createIndex An alias for .
Now in deptno and sal Build an index on , And review the execution plan :db.emp.createIndex({“deptno”:1,“sal”:-1})
Be careful : Except that it can be used explain() Generate execution plan out of plan , There are also several optional parameters , as follows :
.explain(“allPlansExecution”)
.explain(“executionStats”)
.explain(“queryPlanner”)
Two 、 Index type one : Single key index (Single Field)
The single key index is the most common index , And _id Different indexes , Single key indexes are not automatically created .
Prepare the data :
db.testindex1.insert({"_id":1,"zipcode":1034,"location":{state:"NY",city:"New York"}})
Create a single key index on a single column :
db.testindex1.createIndex({"zipcode":1})
Create a single key index on nested Columns
db.testindex1.createIndex({"location:state":1})
Create a single key index on an embedded document
db.testindex1.createIndex({"location":-1})
This will put location As a whole .
3、 ... and 、 Index type two : Multi key index (Multikey Index)
A multi key index is created in the same way as a single key index , The difference is the value of the field . Value has multiple records , Such as arrays .
Pictured above , Create a multi key index based on an array on a collection , And the array is an embedded document .
Prepare the data :
db.testindex2.insertMany([
{ _id: 5, type: "food", item: "aaa", ratings: [ 5, 8, 9 ] },
{ _id: 6, type: "food", item: "bbb", ratings: [ 5, 9 ] },
{ _id: 7, type: "food", item: "ccc", ratings: [ 9, 5, 8 ] },
{ _id: 8, type: "food", item: "ddd", ratings: [ 9, 5 ] },
{ _id: 9, type: "food", item: "eee", ratings: [ 5, 9, 5 ] }])
Based on ratings Column creates a multi key index :
db.testindex2.createIndex( { ratings: 1 } )
Query array for 5,9 Documents
db.testindex2.find( { ratings: [ 5, 9 ] } )
Here's the execution plan
db.testindex2.find( { ratings: [ 5, 9 ] } ).explain()
边栏推荐
- Altaro VM backup getting started
- [trivia of two-dimensional array application] | [simple version] [detailed steps + code]
- Why should we rewrite hashcode when we rewrite the equals method?
- Redhat7 system root user password cracking
- mapbox尝鲜值之云图动画
- 期末复习DAY8
- Xaml gradient issue in uwp for some devices
- The request database reported an error: "could not extract resultset; SQL [n/a]; needed exception is org.hibernate.exception.sqlgram"
- [function explanation (Part 1)] | | knowledge sorting + code analysis + graphic interpretation
- 6.23星期四库作业
猜你喜欢
PHP notes are super detailed!!!
Simpleitk learning notes
Analysis of the example of network subnet division in secondary vocational school
[explain in depth the creation and destruction of function stack frames] | detailed analysis + graphic analysis
2022.DAY592
Redis cannot connect remotely.
Error 1045 (28000) occurs when Linux logs in MySQL: access denied for user 'root' @ 'localhost' (using password: yes)
Brief introduction of realsense d435i imaging principle
"C and pointer" - Chapter 13 advanced pointer int * (* (* (*f) () [6]) ()
Latest version of source insight
随机推荐
一起上水硕系列】Day 9
32GB Jetson Orin SOM 不能刷机问题排查
Talk about how to use p6spy for SQL monitoring
Shanghai daoning, together with American /n software, will provide you with more powerful Internet enterprise communication and security component services
MySQL 5.7.32-winx64 installation tutorial (support installing multiple MySQL services on one host)
Troubleshooting of 32GB Jetson Orin SOM failure to brush
Altaro virtual machine replication failed: "unsupported file type vmgs"
Configure and use Anaconda environment in pycharm
[written examination question analysis] | | get [sizeof and strlen] [pointer and array] graphic explanation + code analysis
Final review (day3)
Get and monitor remote server logs
Redis cannot connect remotely.
[function explanation (Part 2)] | [function declaration and definition + function recursion] key analysis + code diagram
Jetson AGX Orin 平台移植ar0233-gw5200-max9295相机驱动
Explanation of variables, code blocks, constructors, static variables and initialization execution sequence of static code blocks of Ali interview questions
Gan network thought
C 语言文件操作函数大全 (超详细)
Final review (Day7)
Method of finding prime number
About debugging the assignment of pagenum and PageSize of the formal parameter pageweb < T > (i.e. page encapsulation generic) in the controller