当前位置:网站首页>Mongodb index operation
Mongodb index operation
2022-06-24 10:47:00 【Chen Bucheng I】
One . brief introduction
The index of the database is similar to the directory index of the book , With the index , When you read a book, you don't have to look through the whole book , You can jump directly to the target content according to the number of directory pages , Improve reading and query efficiency . So is the index of the database , Its function is to improve the query speed , With the index ,MongoDB When querying, you can find entries in the index , Jump straight to the target collection The location of .
Two . Default index
When retrieving the system index , You can find ,mongodb By default, a default is established for each set ”_id” Indexes , The index used as a reference for retrieval . That is, our usual find All operations are based on the default ”_id” This index is used to find
> db.system.indexes.find();{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "kaiye.c1", "name" : "_id_" }{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "kaiye.c2", "name" : "_id_" }{ "v" : 1, "key" : { "_id" : 1 }, "ns" : "kaiye.c3", "name" : "_id_" }3、 ... and . Why build a custom index
Gather first c4 Add a million pieces of data to , Post retrieval age=100 The data of , And call explain Function to calculate the information in the retrieval process , Found in no sort , Without a custom index , No data was queried , Need to scan onemillion pieces of data , And just a simple query costs 546 millisecond , This is difficult to accept in a business system with a large amount of data .
for(var i=0;i<1000000;i++){ db.c4.insert({name:"diao",age:i})}Four . A comparison between the presence and absence of a custom index
First of all to c4 A collection of name Field indexing , Keyword is ensureIndex, The grammar is db.c4.ensureIndex({age:1}), Then execute the query age=100 The operation of , The retrieval information is shown in the figure below
Now you can see , For age After the fields are indexed, perform a simple query , At this time, the number of traversals is only one , And it takes almost 0, Compared with non indexed Retrieval , The effect is ordinary .
5、 ... and . Other operations on the index
1. Query information about the index db. aggregate .stats();
2. Delete index , Delete the collection , It will also delete all the indexes in the collection db. aggregate .dropIndex({ Index field :1})
3. Build unique index : A unique index means , When a field is set as a unique index , This field is in the set , The same value is not allowed , That is, the value is unique , For example, the above cases , Insert again age=100 A document of , There will be error messages . db. aggregate .ensureIndex({age:1},{unique:true})
边栏推荐
- Stack Title: fractions in parentheses
- Window function row in SQL Server_ number()rank()dense_ rank()
- js中对象合并的4种方式,对象合并的4种方法
- Caching mechanism for wrapper types
- 2022全网最全最细的jmeter接口测试教程以及接口测试流程详解— JMeter测试计划元件(线程<用户>)
- JMeter接口测试工具基础 — Badboy工具
- The nodejs service global timeout callback failed to get process Domain problem
- 线程运行原理
- 26.删除有序数组的重复项
- Six states of threads
猜你喜欢

MYSQL_精讲数据库数据类型

Window function row in SQL Server_ number()rank()dense_ rank()

Flink集群搭建以及企业级yarn集群搭建

26.删除有序数组的重复项

【资源分享】2022年环境工程与生物技术国际会议(CoEEB 2022)

Canvas infinite scan JS special effect code

2022 the most complete and detailed JMeter interface test tutorial and detailed interface test process in the whole network - JMeter test plan component (thread < user >)

Quick completion guide for manipulator (III): mechanical structure of manipulator

Leetcode-2221: triangular sum of arrays

Petit guide de construction rapide du bras mécanique (II): application du bras mécanique
随机推荐
Practice sharing of packet capturing tool Charles
Several stacks of technology sharing: product managers' Online Official answers to several stacks of knowledge
Wechat applet rich text picture width height adaptive method introduction (rich text)
栈题目:函数的独占时间
[Qianfan 618 countdown!] IAAs operation and maintenance special preferential activities
Web project deployment
[resource sharing] 2022 International Conference on Environmental Engineering and Biotechnology (coeeb 2022)
How does easydss use go fastdfs distributed file servers to reduce service pressure?
[data analysis data source] coordinates of provinces, cities and administrative regions across the country (including boundary coordinate points and central coordinate points)
[JS reverse sharing] community information of a website
Learn to use the kindeditor rich text editor. Click to upload a picture. The mask is too large or the white screen solution
What is the knowledge map? What does it do
Canvas infinite scan JS special effect code
2022 International Symposium on intelligent robots and systems (isoirs 2022)
突然想到老家的木屋
【数据分析数据源】全国各省市行政区坐标(包含边界坐标点和中心坐标点)
【Energy Reports期刊发表】2022年能源与环境工程国际会议(CFEEE 2022)
Younger sister Juan takes you to learn JDBC --- 2-day sprint Day1
SSM integration
Distributed transaction principle and solution