zoukankan      html  css  js  c++  java
  • MongoDB的索引

    索引通常能够极大的提高查询的效率,如果没有索引,MongoDB在读取数据时必须扫描集合中的每个文件并选取那些符合查询条件的记录,

    这种扫描全集合的查询效率是非常低的,特别在处理大量的数据时,查询可以要花费几十秒甚至几分钟,这对网站的性能是非常致命的,

    索引是特殊的数据结构,存储在一个易于遍历读取的数据集合中,是对数据库表中一列或多列的值进行排序的一种结构。

    一.创建索引

    >db.collection.createIndex(keys, options)

    keys表示要建立索引的字段和值,字段是索引键,值描述该字段的索引类型,字段的类型为升序索引,请指定值为1降序索引,请指定值为-1

    options可选参数有:

    background Boolean   建索引过程会阻塞其它数据库操作,background可指定以后台方式创建索引,即增加 "background" 可选参数。 "background" 默认值为false。        
    unique Boolean 建立的索引是否唯一。指定为true创建唯一索引。默认值为false.
    name string 索引的名称。如果未指定,MongoDB的通过连接索引的字段名和排序顺序生成一个索引名称。
    sparse Boolean 对文档中不存在的字段数据不启用索引;这个参数需要特别注意,如果设置为true的话,在索引字段中不会查询出不包含对应字段的文档.。默认值为 false.

    1.1:创建单列普通索引

    > db.order.createIndex({content:1})
    {
            "createdCollectionAutomatically" : false,
            "numIndexesBefore" : 1,
            "numIndexesAfter" : 2,
            "ok" : 1
    }

    1.2:创建多列普通索引

    > db.order.createIndex({name:1,cate:1})
    {
            "createdCollectionAutomatically" : false,
            "numIndexesBefore" : 1,
            "numIndexesAfter" : 2,
            "ok" : 1
    }
    > db.order.getIndexes()
    [
            {
                    "v" : 2,
                    "key" : {
                            "_id" : 1
                    },
                    "name" : "_id_",
                    "ns" : "shop.order"
            },
            {
                    "v" : 2,
                    "key" : {
                            "name" : 1,
                            "cate" : 1
                    },
                    "name" : "name_1_cate_1", #将name和cate放到一起创建索引,适用于多条件搜索
                    "ns" : "shop.order"
            }
    ]
    

    1.3:创建唯一索引

    > db.order.createIndex({cate:1},{unique:true})
    {
            "createdCollectionAutomatically" : false,
            "numIndexesBefore" : 1,
            "numIndexesAfter" : 2,
            "ok" : 1
    }
    > db.order.getIndexes()
    [
            {
                    "v" : 2,
                    "key" : {
                            "_id" : 1
                    },
                    "name" : "_id_",
                    "ns" : "shop.order"
            },
            {
                    "v" : 2,
                    "unique" : true,    #唯一索引
                    "key" : {
                            "cate" : 1
                    },
                    "name" : "cate_1",
                    "ns" : "shop.order"
            }
    ]
    

    1.4:创建稀疏索引

    > db.order.createIndex({name:1},{sparse:true})
    {
            "createdCollectionAutomatically" : false,
            "numIndexesBefore" : 1,
            "numIndexesAfter" : 2,
            "ok" : 1
    }
    > db.order.getIndexes()
    [
            {
                    "v" : 2,
                    "key" : {
                            "_id" : 1
                    },
                    "name" : "_id_",
                    "ns" : "shop.order"
            },
            {
                    "v" : 2,
                    "key" : {
                            "name" : 1
                    },
                    "name" : "name_1",
                    "ns" : "shop.order",
                    "sparse" : true  #稀疏索引
            }
    ]
    

    稀疏索引与普通索引的区别:如果有1,2,3,4四条数据,其中第4条数据没有字段email,另外三条都有,那么,是普通索引的话在查询时db.collection.find({email:null})是可以查询到第四条数据的,这就说明普通索引会把没有该索引字段的数据的值定为null;而如果是稀疏索引的话,在查询时查不到第四条数据,这说明稀疏索引将没有该索引字段的数据直接过滤掉不会设定值

    1.5:哈希索引

    > db.order.createIndex({name:'hashed'})
    {
            "createdCollectionAutomatically" : false,
            "numIndexesBefore" : 1,
            "numIndexesAfter" : 2,
            "ok" : 1
    }
    > db.order.getIndexes()
    [
            {
                    "v" : 2,
                    "key" : {
                            "_id" : 1
                    },
                    "name" : "_id_",
                    "ns" : "shop.order"
            },
            {
                    "v" : 2,
                    "key" : {
                            "name" : "hashed"
                    },
                    "name" : "name_hashed",
                    "ns" : "shop.order"
            }
    ]

    二.查看索引

    > db.order.getIndexes()
    [
            {
                    "v" : 2,
                    "key" : {
                            "_id" : 1
                    },
                    "name" : "_id_",
                    "ns" : "shop.order"
            },
            {
                    "v" : 2,
                    "key" : {
                            "content" : 1
                    },
                    "name" : "content_1",
                    "ns" : "shop.order"
            }
    ]
    

    普通索引已经创建成功,_id为mongo的默认索引字段

    三.删除索引

    > db.order.dropIndex({"content":1})  #删除指定索引
    { "nIndexesWas" : 2, "ok" : 1 }
    
    > db.order.dropIndexes()    #删除所有索引
    {
            "nIndexesWas" : 1,
            "msg" : "non-_id indexes dropped for collection",
            "ok" : 1
    }
    

    四.重建索引

    > db.order.reIndex()
    {
            "nIndexesWas" : 2,
            "nIndexes" : 2,
            "indexes" : [
                    {
                            "v" : 2,
                            "key" : {
                                    "_id" : 1
                            },
                            "name" : "_id_",
                            "ns" : "shop.order"
                    },
                    {
                            "v" : 2,
                            "key" : {
                                    "name" : "hashed"
                            },
                            "name" : "name_hashed",
                            "ns" : "shop.order"
                    }
            ],
            "ok" : 1
    }
    

    可以通过索引的重建减少索引文件碎片提高索引的效率,对于大多数用户来说reIndex()操作是不必要的,避免针对副本集中的集合运行,对于副本集reIndex()不会从主节点传播到副节点,不要针对分片群集中的集合运行,reIndex只会影响单个mongo实例.

  • 相关阅读:
    Windows 下安装 Python环境安装
    关于form表单提交ajaxForm和ajaxSubmit的用法与区别
    .NET Core Runtime 和 .NET Core SDK
    路由表中没有与提供的值匹配的路由
    SQL server Cannot find one or more
    CentOS7安装完毕,重新开机启动后显示: Initial setup of CentOS Linux 7 (core)
    private static
    接口和抽象类
    static const readonly
    frameset的使用小结
  • 原文地址:https://www.cnblogs.com/52lnamp/p/10191510.html
Copyright © 2011-2022 走看看