美文网首页
mongo回顾(四)

mongo回顾(四)

作者: supremecsp | 来源:发表于2021-04-10 22:31 被阅读0次

上回聊到mongo索引采用了B树,而且采用的原因。今天具体聊聊mongo中的索引。
MongoDB defines indexes at the collection level and supports indexes on any field or sub-field of the documents in a MongoDB collection.
mongo的索引是文档级别的,支持在字段和子字段建立索引
1,首先是_id索引,mongo的_id索引是唯一的,默认ObjectId的组成为


image.png

( 新版ObjectId中“机器标识码+进程号” 改为用随机数作为机器标识和进程号的值)


image.png

唯一索引问题 如果集群在_id上进行了分片,则无法再在其他字段上建立唯一索引:之所以出现这个错误是因为MongoDB无法保证集群中除了片键以外其他字段的唯一性(验证了CAP理论),能保证片键的唯一性是因为文档根据片键进行切分,一个特定的文档只属于一个分片,MongoDB只要保证它在那个分片上唯一就在整个集群中唯一。
分片只能保证片键的唯一,若_id不是片键,则需要人为保证唯一性
创建索引简单语法:db.collection.createIndex( { name: -1 } ),value 0为升序,-1为降序
You can view index names using the db.collection.getIndexes() method. You cannot rename an index once created. Instead, you must drop and re-create the index with a new name.
简历索引后无法重命名,要重命名得删除索引重建(命名是name字段)

索引类型分为
1,单字段索引(除_id外的其他字段建立索引)
2,复合索引{ userid: 1, score: -1 }

image.png
https://docs.mongodb.com/manual/core/index-compound/#std-label-index-ascending-and-descending
排序操作是否能使用复合索引得看是怎么样的排序,按上面的索引
db.events.find().sort( { username: 1, date: 1 } )用不到{ "username" : 1, "date" : -1 }索引,具体请看https://docs.mongodb.com/manual/tutorial/sort-results-with-indexes/
而且复合索引也有类型mysql的最左匹配原则
3,多键索引(存储数组中的内容)
image.png
对于多键索引,在这里只提一点
To compound together the bounds for index keys from the same array:
the index keys must share the same field path up to but excluding the field names, and
the query must specify predicates on the fields using $elemMatch on that path.
同一数组的不同字段要正确使用索引分界,得加上$elemMatch
https://docs.mongodb.com/manual/core/multikey-index-bounds/
4,地理空间索引(我还没用过)
5,全文索引(对字符串内容搜索查询,一个文档只能有一个文本索引)
分隔符为空格和标点符号
For tokenization, version 3 text index uses the delimiters categorized under Dash, Hyphen, Pattern_Syntax, Quotation_Mark, Terminal_Punctuation, and White_Space in Unicode 8.0 Character Database Prop List.
文本索引需要比较大的性能开销,使用时做好选择(https://docs.mongodb.com/manual/core/index-text/)
(https://zhuanlan.zhihu.com/p/100820537)
6,哈希索引(用于片键,分散数据在不同分片,不支持范围查询)

索引属性分为
1,唯一索引
2,部分索引
db.restaurants.createIndex(
{ cuisine: 1, name: 1 },
{ partialFilterExpression: { rating: { $gt: 5 } } }
)
对rating字段大于5的字段建立索引,自由度高,可以减少索引树的数据量,提高性能
使用部分索引进行查询时,记得加上过滤语句,不然会使索引无法正常使用
db.restaurants.find( { cuisine: "Italian", rating: { $gt: 8 } } )可以用上索引
db.restaurants.find( { cuisine: "Italian" } )用不上
部分索引可以实现稀疏索引功能
db.contacts.createIndex(
{ name: 1 },
{ partialFilterExpression: { name: { $exists: true } } }
)
限制:id与片键不能当做部分索引
_id indexes cannot be partial indexes.
Shard key indexes cannot be partial indexes.
3.稀疏索引
没有改字段就跳过该文档,空值也跳过,部分索引包括稀疏索引,官方建议
If you are using MongoDB 3.2 or later, partial indexes should be preferred over sparse indexes.
4,TTL索引
好用,设置过期时间删除文档
db.eventlog.createIndex( { "lastModifiedDate": 1 }, { expireAfterSeconds: 3600 } )
或者特定时间使文档过期
db.log_events.createIndex( { "expireAt": 1 }, { expireAfterSeconds: 0 } )
https://docs.mongodb.com/manual/tutorial/expire-data/
有个知识点提一下,redis的过期机制是惰性删除+定期删除,所以到期了数据就拿不到了,mongo的过期机制是每隔60S去删除到期的数据,所以还是有机会在60S内拿到过期的数据
限制:
复合索引不能使用TTL
一个索引没有TTL,不能直接加上,需要删除重建
TTL时间更改,也需要删除重建或者collMod
总结:mongodb 索引优点,索引种类比关系型数据库多了不少,选择面,支持面也比较大
最后:由于索引涉及知识点比较多,推荐大家去看官方文档,这里只做最简单的介绍
(https://docs.mongodb.com/manual/core/index-text/)

相关文章

  • mongo回顾(四)

    上回聊到mongo索引采用了B树,而且采用的原因。今天具体聊聊mongo中的索引。MongoDB defines ...

  • mongo回顾(五)

    上一篇聊到了索引类型和索引属性,今天来聊聊索引的性能分析首先说说索引的分析工具explain,我常用的工具是Mon...

  • mongo回顾(一)

    这个月轮到我做技术分享,由于关系型数据库被分享了好几次了,那我就挑个非关系型的来分享吧,分享之前对mongo做一些...

  • mongo回顾(二)

    上回提到,对比关系型数据库的全部,非关系型数据库就像偏科选手。但是非关系型数据库也有全能型选手,那就是mongod...

  • The Road of DBA 19_NoSQL_Elastic

    mongo回顾 2、Elasticsearch介绍 2.1 2.2、Elasticsearch功能 2.3、特点 ...

  • mongo回顾(十:日志)

    上一篇留下了一个疑问,mongo是内存数据库吗? 我们知道,redis是内存数据库,数据都保存在内存中,对于数据的...

  • Timed out after 30000 ms while w

    今天使用mongo-java-drive写连接mongo的客户端,着实被上面那个错坑了一把。回顾一下解决过程: 报...

  • mongo回顾(七:聚合查询)

    上一篇简单介绍了mongo的增删改查,今天来聊聊mongo的聚合操作 什么是 MongoDB 聚合框架 Mongo...

  • mongo回顾(十四:复制集)

    首先简单的搭建mongodb复制集(一主两从),依旧采用docker1,docker pull mongo2,do...

  • mongo回顾(十二:原子性)

    技术分享之后好久没写简书了,接下来会把我所知道的mongo知识点补完,这篇文章主要聊聊mongodb的事务 mon...

网友评论

      本文标题:mongo回顾(四)

      本文链接:https://www.haomeiwen.com/subject/vxrokltx.html