当前位置:网站首页>Mongodb index (3)
Mongodb index (3)
2022-07-29 02:52:00 【(illusion)】
About index
Index support in MongoDB Efficiently execute queries in . If there is no index ,MongoDB A full set scan must be performed , That is, scanning every document in the collection , To select and query statements
Matching documents . The query efficiency of scanning the whole collection is very low , Especially when dealing with large amounts of data , Queries can take tens of seconds or even minutes , This is right and wrong for the performance of the website
Often fatal .
If the query has an appropriate index ,MongoDB You can use this index to limit the number of documents that must be checked .
An index is a special data structure , It stores a small portion of the collection dataset in an easy to traverse form . An index stores the values of a particular field or group of fields , Sort by field value . Row of index entries
Order supports effective equality matching and range based query operations . Besides ,MongoDB You can also use the sort in the index to return the sort result .MongoDB Index usage B Tree data structure ( Exactly B-Tree,MySQL yes B+Tree)
Type of index
1. Single field index
MongoDB Supports the creation of user-defined ascending order on a single field of a document / descending index , It's called a single field index (Single Field Index). For single field indexing and sorting operations , Sort order of index keys ( In ascending or descending order ) Not important , because MongoDB The index can be traversed in any direction
Composite index
MongoDB It also supports user-defined indexes for multiple fields , Composite index (Compound Index). The order of the fields listed in a composite index is significant . for example , If the composite index is from { userid: 1, score: -1 } form , Then the index first presses userid Positive sequence sort , then At every userid Within the value of , Press... Again score Reverse sort .
Other indexes
Geospatial index (Geospatial Index)、 Text index (Text Indexes)、 Hash index (Hashed Indexes).
Geospatial index (Geospatial Index)
In order to support the effective query of geospatial coordinate data ,MongoDB Two special indexes are provided : Two dimensional index of planar geometry is used to return the result and spherical surface is used to return the result
Two dimensional spherical index of Geometry .Text index (Text Indexes)
MongoDB Provides a type of text index , Support searching string content in the collection . These text indexes don't store language specific stop words ( for example “the”、“a”、“or”),
And the words in the set are used as stems , Store only the root word .Hash index (Hashed Indexes)
To support hash based fragmentation ,MongoDB Provides a hash index type , It indexes a hash of field values . These indexes have a more random distribution of values within their ranges , But only
Equal matching , Range based queries are not supported .
Index management
1. Look at the index
Returns an array of all indexes in a collection : db.collection.getIndexes()
MongoDB In the process of creating a collection , stay id Create a unique index on the field , The default name is _id , This index prevents the client from inserting two texts with the same value files , You can't be in _id Delete this index from the field .
The index is unique , So the value cannot be repeated , namely _id The value cannot be repeated . In a shard cluster , Usually use _id As a slice key .
Index creation :
db.collection.createIndex(keys, options)
Parameter | Type | Description |
---|---|---|
background | Boolean | The indexing process blocks other database operations ,background You can specify backchannel mode to create indexes , That is, increase "background" Optional parameters . "background" The default value is false. |
unique | Boolean | Is the index created unique . Designated as true Create unique index . The default value is false. |
name | string | Name of index . If not specified ,MongoDB Generate an index name by connecting the field name and sort order of the index |
dropDups | Boolean | 3.0+ Version obsolete . Whether to delete duplicate records when establishing a unique index , Appoint true Create unique index . The default value is false. |
sparse | Boolean | Indexes are not enabled for field data that does not exist in the document ; This parameter requires special attention , If set to true Words , On the rope Documents that do not contain corresponding fields will not be queried in the reference field .. The default value is false. |
expireAfterSeconds | integer | Specifies a value in seconds , complete TTL Set up , Sets the lifetime of the collection . |
v | index version | The version number of the index . The default index version depends on mongod The version that runs when the index is created . |
weights | document | Index weight , Values in 1 To 99,999 Between , Represents the score weight of the index relative to other index fields . |
default_language | string | For text index , This parameter determines the list of stop words and the rules for the word stem and lexicon . Default to English |
language_override | string | For text index , This parameter specifies the name of the field to be included in the document , The language overrides the default language, The default value is language. |
Single field index :
db.comment.createIndex({userid:-1})
Yes userid Field indexing
Composite index
db.comment.createIndex({userid:-1,nickname:1})
Index removal
db.collection.dropIndex(index)
Parameter | Type | Description |
---|---|---|
index | string or document | Specify the index to delete . The index can be specified by index name or index specification document . To delete the text index , Please specify The index name . |
Use of index
db.collection.find(query,options).explain(options)
Add index :
Cover queries
When the query condition and query projection contain only index fields ,MongoDB Return results directly from the index , Without scanning any documents or bringing them into memory . These overridden queries can Very effective .
边栏推荐
猜你喜欢
php 进程通信系列 (一) 命名管道
每周推荐短视频:如何让产品开发更加有效?
第七天笔记
Multimodal unsupervised image to image translation
解析机器人与人类情感共鸣的主观意识
This blogger has a comprehensive classification of QT. If you are free, go to study and summarize it and record it.
第2章 VRP命令行
Thirty years of MPEG audio coding
New conch movie theme template m3.1 fully decrypted version multifunctional apple cmsv10 background adaptive theme open source fully decrypted version
第八天笔记
随机推荐
Some new ideas about time complexity
qt QStringList用法
C语言:小乐乐与进制转换
用于校园流浪猫信息记录和分享的小程序源码/微信云开发中大猫谱小程序源码
MPEG音频编码三十年
《微信小程序-进阶篇》Lin-ui组件库源码分析-Button组件(二)
常用hooks总结
解析Steam教育中的项目式学习创造力
RestfulAPI——C#——增加header用户名密码验证
Tesla neural network model hydranet
自动分账系统哪家好?
QT屏幕自适应自动布局,拖动窗口自动变大变小(一)
第五天实验
云开发口袋工具箱微信小程序源码
自组织是管理者和成员的双向奔赴
R语言ERROR: compilation failed for package ‘****‘
QT qstringlist usage
nacos名字的由来
外挂---线段树懒标记板子+简单数学推理
php 进程通信系列 (一) 命名管道