MongoDB 复合索引

本文涉及的产品
云数据库 MongoDB,通用型 2核4GB
简介: MongoDB支持复合索引,即将多个键组合到一起创建索引。该方式称为复合索引,或者也叫组合索引,该方式能够满足多键值匹配查询使用索引的情形。

MongoDB支持复合索引,即将多个键组合到一起创建索引。该方式称为复合索引,或者也叫组合索引,该方式能够满足多键值匹配查询使用索引的情形。其次复合索引在使用的时候,也可以通过前缀法来使用索引。MongoDB中的复合索引与关系型数据库基本上一致。在关系型数据库中复合索引使用的一些原则同样适用于MongoDB。本文主要描述MongoDB复合索引。

一、复合索引相关概述

1、复合索引创建语法
        db.collection.createIndex( { <field1>: <type>, <field2>: <type2>, ... } )
        同创建单键(列)索引一样,索引创建时需要指定每一个键索引的顺序
        多个键直接用逗号分隔
        索引创建语法可以参考:http://blog.csdn.net/leshami/article/details/53541978

2、复合索引的一些特性
        复合索引可以支持要求匹配多个键的查询
        复合索引每一个键的顺序非常重要,这将决定该索引在查询过程中能否被使用到
        复合索引支持前导(缀)列索引查询
        不能够创建基于哈希索引类型的复合索引
        任意复合索引字段不能超过31

二、复合索引示意图

如下图所示,在集合的userid以及score列上创建一个复合索引,其中userid为升序,score为降序
这里写图片描述

三、复合索引示例

1、演示环境

> db.version()
3.2.10
> db.example.find({},{"_id":0})
{ "id" : 1, "ename" : "leshami", "blog" : "http://blog.csdn.net/leshami", "name" : "leshami" }

演示集合数据,可以参考:http://blog.csdn.net/leshami/article/details/52672310

//查看任意的一个文档
> db.persons.find().limit(1).pretty()
{
        "_id" : ObjectId("5812cbaaa129eed14b46458d"),
        "name" : "robinson.cheng",
        "age" : 25,
        "email" : "robinson.cheng@qq.com",
        "score" : {
                "c" : 89,
                "m" : 96,
                "e" : 87
        },
        "country" : "USA",
        "books" : [
                "JS",
                "C++",
                "EXTJS",
                "MONGODB"
        ]
}

2、创建复合索引

//如下示例,我们在集合persons上的name及age键上创建复合索引,且2个都为升序
> db.persons.createIndex({name:1,age:1})
{
        "createdCollectionAutomatically" : false,
        "numIndexesBefore" : 1,
        "numIndexesAfter" : 2,
        "ok" : 1
}

//在上面的示例中索引首先会按照name的值升序进行排列
//其次是age键,在name之后也按照升序排列

//下面过滤条件仅使用一个name键来查看执行计划
> db.persons.find({name:"robinson.cheng"}).explain()
{
   ......
   "stage" : "FETCH",
   "inputStage" : {
           "stage" : "IXSCAN",  //使用索引扫描
           "keyPattern" : {
                   "name" : 1,
                   "age" : 1
           },
           "indexName" : "name_1_age_1",
             ......
           "direction" : "forward",
           "indexBounds" : {
                   "name" : [
                           "[\"robinson.cheng\", \"robinson.cheng\"]"
                   ],
                   "age" : [
                           "[MinKey, MaxKey]"
  .........
 "ok" : 1
}

//下面过滤条件仅使用name及age键来查看执行计划
> db.persons.find({name:"robinson.cheng",age:25}).explain()
{
                 ......... 
                 "stage" : "FETCH",
                 "inputStage" : {
                         "stage" : "IXSCAN",  //使用索引扫描
                         "keyPattern" : {
                                 "name" : 1,
                                 "age" : 1
                         },
                         "indexName" : "name_1_age_1",
                          .........
                         "direction" : "forward",
                         "indexBounds" : {
                                 "name" : [
                                         "[\"robinson.cheng\", \"robinson.cheng\"]"
                                 ],
                                 "age" : [
                                         "[25.0, 25.0]"
          ...........
 "ok" : 1
}

//下面过滤条件仅使用name及age键来查看执行计划,但是将age键放在name键之前
> db.persons.find({age:25,name:"robinson.cheng"}).explain()
{
 "queryPlanner" : {
      .....
         "winningPlan" : {
                 "stage" : "FETCH",
                 "inputStage" : {
                         "stage" : "IXSCAN",  //使用索引扫描
                         "keyPattern" : {
                                 "name" : 1,
                                 "age" : 1
                         },
                         "indexName" : "name_1_age_1",
                          ...........
                         "direction" : "forward",
                         "indexBounds" : {
                                 "name" : [
                                         "[\"robinson.cheng\", \"robinson.cheng\"]"
                                 ],
                                 "age" : [
                                         "[25.0, 25.0]"
       ........
 "ok" : 1
}

//下面单独基于age键作为过滤条件进行查询
> db.persons.find({age:25}).explain()
{
         ................
                "winningPlan" : {
                        "stage" : "COLLSCAN",  //此处为使用集合扫描方式
                        "filter" : {
                                "age" : {
                                        "$eq" : 25
                                }
                        },
                        "direction" : "forward"
                },
                "rejectedPlans" : [ ]
         ..............
        "ok" : 1
}

3、复合索引与排序

复合索引创建时按升序或降序来指定其排列方式。对于单键索引,其顺序并不是特别重要,因为MongoDB可以在任一方向遍历索引
对于复合索引,按何种方式排序能够决定该索引在查询中能否被使用到。

//以下内容基于前面在{name:1,age:1}键上创建的索引来考察这个复合索引在排序时被使用到的场景
//基于{name:1,age:1}的排序
> db.persons.find().sort({name:1,age:1}).explain()
{
    "queryPlanner" : {
            ....
            "winningPlan" : {
                    "stage" : "FETCH",
                    "inputStage" : {
                            "stage" : "IXSCAN",   //索引扫描
                            "keyPattern" : {
                                    "name" : 1,
                                    "age" : 1
                            },
                            "indexName" : "name_1_age_1",
                             ..........
                            "direction" : "forward",
                            "indexBounds" : {
                                    "name" : [
                                            "[MinKey, MaxKey]"
                                    ],
                                    "age" : [
                                            "[MinKey, MaxKey]"
         ....
    "ok" : 1
}

//基于{name:1,age:-1}的排序
> db.persons.find().sort({name:1,age:-1}).explain()
{
    "queryPlanner" : {
      ....
            "winningPlan" : {
                    "stage" : "SORT",
                    "sortPattern" : {
                            "name" : 1,
                            "age" : -1
                    },
                    "inputStage" : {
                            "stage" : "SORT_KEY_GENERATOR",
                            "inputStage" : {
                                    "stage" : "COLLSCAN", //集合扫描
                                    "filter" : {
                                            "$and" : [ ]
                                    },
                                    "direction" : "forward"
       .........
    "ok" : 1
}

//基于{name:-1,age:1}的排序
> db.persons.find().sort({name:-1,age:1}).explain()
{
   "queryPlanner" : {
           .........
           "winningPlan" : {
           "stage" : "SORT",
           "sortPattern" : {
                   "name" : -1,
                   "age" : 1
           },
           "inputStage" : {
                   "stage" : "SORT_KEY_GENERATOR",
                   "inputStage" : {
                           "stage" : "COLLSCAN",  //集合扫描
                           "filter" : {
                                   "$and" : [ ]
                           },
                           "direction" : "forward"
      .........
   "ok" : 1
}

//基于{age:1,name:1}的排序
> db.persons.find().sort({age:1,name:1}).explain()
{
          ....
         "inputStage" : {
          "stage" : "SORT_KEY_GENERATOR",
          "inputStage" : {
                  "stage" : "COLLSCAN",  //集合扫描
                  "filter" : {
                          "$and" : [ ]
                  },
                  "direction" : "forward"
         ..........
  "ok" : 1
}

//基于{age:-1,name:1}的排序
> db.persons.find().sort({age:-1,name:1}).explain()
{
   ..........
   "inputStage" : {
           "stage" : "SORT_KEY_GENERATOR",
           "inputStage" : {
                   "stage" : "COLLSCAN",    //集合扫描
                   "filter" : {
                           "$and" : [ ]
                   },
                   "direction" : "forward"
   ..........
  "ok" : 1
}

//基于{name:-1,age:-1}的排序
> db.persons.find().sort({name:-1,age:-1}).explain()
{
      .........
      "inputStage" : {
              "stage" : "IXSCAN",      //索引扫描
              "keyPattern" : {
                      "name" : 1,
                      "age" : 1
              },
              "indexName" : "name_1_age_1",
              ............ 
              "direction" : "backward", //注意,这里的方向为向后扫描
              "indexBounds" : {
                      "name" : [
                              "[MaxKey, MinKey]"
                      ],
                      "age" : [
                              "[MaxKey, MinKey]"
    ......
 "ok" : 1
}

通过上面的不同场景,得出如下:
排序使用到索引的情形
        db.persons.find().sort({name:1,age:1})
        db.persons.find().sort({name:-1,age:-1})

排序未使用到索引的情形
        db.persons.find().sort({name:1,age:-1})
        db.persons.find().sort({name:-1,age:1})
        db.persons.find().sort({age:1,name:1})
        db.persons.find().sort({age:-1,name:1})

4、复合索引与索引前缀

    索引前缀指的是复合索引的子集
    假如存在如下索引

    { "item": 1, "location": 1, "stock": 1 }

    那存在下列索引前缀
    { item: 1 }
    { item: 1, location: 1 }

    在MongoDB中,下列查询过滤条件情形中,索引将会被使用到
            item字段
            item字段 + location字段
            item字段 + location字段 + stock字段
            item字段 + location字段(尽管索引被使用,但不高效)

    以下过滤条件查询情形,索引将不会被使用到
            location字段
        stock字段
        location + stock字段  

5、小结
a、复合索引是基于多个键(列)上创建的索引
b、复合索引在创建的时候可以为其每个键(列)来指定排序方法
c、索引键列的排序方法影响查询在排序时候的操作,方向一致或相反的才能被匹配
d、复合索引与前缀索引通常在匹配的情形下才能被使用

DBA牛鹏社(SQL/NOSQL/LINUX)

相关实践学习
MongoDB数据库入门
MongoDB数据库入门实验。
快速掌握 MongoDB 数据库
本课程主要讲解MongoDB数据库的基本知识,包括MongoDB数据库的安装、配置、服务的启动、数据的CRUD操作函数使用、MongoDB索引的使用(唯一索引、地理索引、过期索引、全文索引等)、MapReduce操作实现、用户管理、Java对MongoDB的操作支持(基于2.x驱动与3.x驱动的完全讲解)。 通过学习此课程,读者将具备MongoDB数据库的开发能力,并且能够使用MongoDB进行项目开发。 &nbsp; 相关的阿里云产品:云数据库 MongoDB版 云数据库MongoDB版支持ReplicaSet和Sharding两种部署架构,具备安全审计,时间点备份等多项企业能力。在互联网、物联网、游戏、金融等领域被广泛采用。 云数据库MongoDB版(ApsaraDB for MongoDB)完全兼容MongoDB协议,基于飞天分布式系统和高可靠存储引擎,提供多节点高可用架构、弹性扩容、容灾、备份回滚、性能优化等解决方案。 产品详情: https://www.aliyun.com/product/mongodb
目录
相关文章
|
3月前
|
存储 NoSQL 关系型数据库
|
6月前
|
NoSQL MongoDB 索引
【最佳实践】MongoDB导入数据时重建索引
【最佳实践】MongoDB导入数据时重建索引
168 0
|
6月前
|
NoSQL MongoDB 索引
开心档-软件开发入门之MongoDB 覆盖索引查询
开心档-软件开发入门之MongoDB 覆盖索引查询
45 0
|
16天前
|
NoSQL MongoDB 索引
【MongoDB】MongoDB 覆盖索引
【4月更文挑战第3天】【MongoDB】MongoDB 覆盖索引
|
3月前
|
存储 NoSQL 关系型数据库
4-MongoDB索引知识
MongoDB索引知识
|
3月前
|
NoSQL MongoDB 索引
【待完善】MongoDB - 使用索引
【待完善】MongoDB - 使用索引
30 0
|
3月前
|
存储 NoSQL MongoDB
MongoDB之索引和聚合
【1月更文挑战第21天】 一、索引 1、说明 2、原理 3、相关操作 3.1、创建索引 3.2、查看集合索引 3.3、查看集合索引大小 3.4、删除集合所有索引(不包含_id索引) 3.5、删除集合指定索引 4、复合索引 二、聚合 1、说明 2、使用
65 0
|
5月前
|
存储 NoSQL Cloud Native
mongodb 索引实操
mongodb 索引实操
|
5月前
|
存储 NoSQL MongoDB
数据库系列课程(23)-MongoDB 索引
数据库系列课程(23)-MongoDB 索引
76 0
|
6月前
|
NoSQL MongoDB iOS开发
开心档-软件开发入门之MongoDB 覆盖索引查询
开心档-软件开发入门之MongoDB 覆盖索引查询