跳至主要內容

3.7 索引 🎉

刘春龙...大约 3 分钟数据库mongodb

3.7 索引 🎉

索引是一种单独的、物理的对数据库表中一列或多列的值进行排序的一种存储结构,它是某个表中一列或若干列值的集合和相应的指向表中物理标识这些值的数据页的逻辑指针清单。索引的作用相当于图书的目录,可以根据目录中的页码快速找到所需的内容。索引目标是提高数据库的查询效率,没有索引的话,查询会进行全表扫描(scan every document in a collection),数据量大时严重降低了查询效率。默认情况下Mongo在一个集(collection)创建时,自动地对集合的_id创建了唯一索引。

单键索引 💎

MongoDB默认所有的集合在_id字段上有一个索引。

下图是一个在score字段上建立的升序/降序索引示意图。

创建一个名为j3的集合,并插入下面的数据

db.j3.insertOne({
 "score": 356,
 "location": { province: "Hebei", city: "Tangshan" }
})

创建索引,1代表升序,-1代表降序

db.j3.createIndex( { score: 1 } )

在内嵌字段上建立索引

db.j3.createIndex( { "location.province": 1 } )

在内嵌文档上建立索引

db.j3.createIndex( { location: 1 } )

复合索引 💎

复合索引是指单个索引结构指向多个字段,下图展示了在两个字段上建立索引

创建一个名为j3的集合,并插入下面的数据

db.j3.insertOne({
 "item": "Banana",
 "category": ["food", "produce", "grocery"],
 "location": "4th Street Store",
 "stock": 4,
 "type": "cases"
})

在item和stock字段上建立升序索引

db.j3.createIndex( { "item": 1, "stock": 1 } )

多键索引 💎

多键索引用于为数组中的元素创建索引

先创建集合j2,使用下面的数据来创建多键索引

db.j2.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 ] }
])

在ratings建立多键索引

db.j2.createIndex( { ratings: 1 } )

MongoDB使用多键索引查找在“ratings”数组中有“5”的文档,然后,MongoDB检索这些文档并筛选“ratings”数组等于查询数组“[5,9]”的文档。

db.j2.find( { ratings: [ 5, 9 ] } )

地理坐标索引 💎

针对地理空间坐标数据创建索引。2dsphere索引用于存储和查找球面上的点,2d索引用于存储和查找平面上的点。

db.j2.insert({
    loc : { type: "Point", coordinates: [ 116.502451, 40.014176 ] },
    name: "军博地铁",
    category : "Parks"
})

创建球形地理坐标索引

db.j2.createIndex( { loc : "2dsphere" } )
db.j2.find({
    "loc" : {
        "$geoWithin" : {
            "$center":[[116.482451,39.914176],0.05]
        }
    }
})

再看平面上的点

db.j1.insert({"name":"aa","addr":[32,32]})
db.j1.insert({"name":"bb","addr":[30,22]})
db.j1.insert({"name":"cc","addr":[28,21]})
db.j1.insert({"name":"dd","addr":[34,26]})
db.j1.insert({"name":"ee","addr":[34,27]})
db.j1.insert({"name":"ff","addr":[39,28]})

创建平面地理坐标索引

db.j1.find({})
db.j1.createIndex({"addr":"2d"})
db.j1.find({"addr":{"$within":{"$box":[[0,0],[30,30]]}}})

全文索引 💎

MongoDB提供了针对string内容的文本查询,Text Index支持任意属性值为string或string数组元素的索引查询。注意:一个集合仅支持最多一个Text Index,中文分词不理想推荐ES

db.j1.insert({name:"aa",description:"no pains,no gains"})
db.j1.insert({name:"ab",description:"pay pains,get gains"})
db.j1.insert({name:"ac",description:"a friend in need,a friend in deed"})

创建索引并指定语言

db.j1.createIndex(
  { description : "text" },
  { default_language: "english" }
)
db.j1.find({"$text": {"$search": "pains"}})

全文索引名称

db.j1.createIndex(
  {
   content: "text",
   "users.comments": "text",
   "users.profiles": "text"
  }
)

生成的默认索引名:content_text_users.comments_text_users.profiles_text,太长了,不方便,所以需要指定名称

db.j1.createIndex(
  {
   content: "text",
   "users.comments": "text",
   "users.profiles": "text"
  },
  {
   name: "MyTextIndex"
  }
)

使用指定名称删除索引

db.j1.dropIndex("MyTextIndex")

hash索引 💎

针对属性的哈希值进行索引查询,当要使用Hashed index时,MongoDB能够自动的计算hash值,无需程序计算hash值。注:hash index仅支持等于查询,不支持范围查询。

db.j1.createIndex({"field": "hashed"})

创建复合hash索引,4.4以后的版本

db.collection.createIndex( { "fieldA" : 1, "fieldB" : "hashed", "fieldC" : -1 } )

索引管理 💎

获取针对某个集合的索引

db.collection.getIndexes()

索引的大小

db.collection.totalIndexSize()

索引的重建

db.collection.reIndex()

索引的删除,注意: _id 对应的索引是删除不了的

db.collection.dropIndex("INDEX-NAME")
db.collection.dropIndexes()
上次编辑于:
贡献者: 刘春龙
评论
  • 按正序
  • 按倒序
  • 按热度
Powered by Waline v2.15.7