zoukankan      html  css  js  c++  java
  • mongodb 稀疏索引

    稀疏索引(或者称间隙索引)就是只包含有索引字段的文档的条目,即使索引字段包含一个空值。也就是说间隙索引可以跳过那些索引键不存在的文档。因为他并非包含所有的文档,因此称为稀疏索引。与之相对的非稀疏索引或者说普通索引则包含所有的文档以及为那些不包含索引的字段存储null值。

    一、间隙索引创建描述

        稀疏索引(或者称间隙索引)就是只包含有索引字段的文档的条目,跳过索引键不存在的文档
        本文中后面的描述使用间隙索引
    
        创建索引的语法:
        db.collection.createIndex(keys, options)
    
        创建间隙索引示例:
                db.addresses.createIndex( { "xmpp_id": 1 }, { sparse: true } )
                这个示例,哪些不包含xmpp_id的键(列)的文档将不会被索引
    
        间隙索引不会被使用到的情形
                如果一个间隙索引会导致查询或者排序操作得到一个不完整结果集的时候,MongoDB将不会使用这个索引,hint提示除外
    
        哪些索引缺省情况就是间隙索引              
                2dsphere (version 2), 2d, geoHaystack, 文本索引等总是稀疏索引
    
        间隙索引与唯一性
                一个既包含稀疏又包含唯一的索引避免集合上存在一些重复值得文档,但是允许多个文档忽略该键。
    

    二、间隙索引示例

    1、创建间隙索引

        > db.version()
        3.2.10
    
        > db.scores.insertMany([
        { "_id" : ObjectId("523b6e32fb408eea0eec2647"), "userid" : "newbie" },
        { "_id" : ObjectId("523b6e61fb408eea0eec2648"), "userid" : "abby", "score" : 82 },
        { "_id" : ObjectId("523b6e6ffb408eea0eec2649"), "userid" : "nina", "score" : 90 }])
    
        //下面为score键创建稀疏索引
        > db.scores.createIndex( { score: 1 } , { sparse: true } )
    
        > db.scores.find( { score: { $lt: 90 } } )
        { "_id" : ObjectId("523b6e61fb408eea0eec2648"), "userid" : "abby", "score" : 82 }
        //由于文档newbie并不包含score键,因此该文档不会出现在稀疏索引之中,也就不会被查询返回
    
        > //下面查询socre小于90文档的执行计划
        > db.scores.find( { score: { $lt: 90 } } ).explain()
        {
                "queryPlanner" : {
                        "plannerVersion" : 1,
                        "namespace" : "test.scores",  //Author : Leshami
                        "indexFilterSet" : false,     //Blog   : http://blog.csdn.net/leshami
                        "parsedQuery" : {
                                "score" : {
                                        "$lt" : 90
                                }
                        },
                        "winningPlan" : {
                                "stage" : "FETCH",
                                "inputStage" : {
                                        "stage" : "IXSCAN", //使用到了索引扫描
                                        "keyPattern" : {
                                                "score" : 1
                                        },
                                        "indexName" : "score_1",  //索引为score_1
                                        "isMultiKey" : false,
                                        "isUnique" : false,
                                        "isSparse" : true,        //此处表名为间隙索引
                                        "isPartial" : false,
                                        "indexVersion" : 1,
                                        "direction" : "forward",
                                        "indexBounds" : {
                                                "score" : [
                                                        "[-inf.0, 90.0)"
                    ...........
                "ok" : 1
        }
    

    2、间隙索引无法使用的示例

        > db.scores.find().sort( { score: -1 } )
        { "_id" : ObjectId("523b6e6ffb408eea0eec2649"), "userid" : "nina", "score" : 90 }
        { "_id" : ObjectId("523b6e61fb408eea0eec2648"), "userid" : "abby", "score" : 82 }
        { "_id" : ObjectId("523b6e32fb408eea0eec2647"), "userid" : "newbie" }
    
        //从上面的查询结果可知,基于索引列score的排序返回了所有的文档
        //这个排序真实的执行计划则是全表扫描,因为索引键并不包含不存在的用户id为newbie的文档
        > db.scores.find().sort( { score: -1 } ).explain()
        {
                "queryPlanner" : {
                        "plannerVersion" : 1,
                        "namespace" : "test.scores",
                        "indexFilterSet" : false,
                        "parsedQuery" : {
                                "$and" : [ ]
                        },
                        "winningPlan" : {
                                "stage" : "SORT",
                                "sortPattern" : {
                                        "score" : -1
                                },
                                "inputStage" : {
                                        "stage" : "SORT_KEY_GENERATOR",
                                        "inputStage" : {
                                                "stage" : "COLLSCAN", //使用了集合扫描方式
                                                "filter" : {
                                                        "$and" : [ ]
                                                },
                                                "direction" : "forward"
                                        }
                ............
                "ok" : 1
        }
    

    3、强制间隙索引的示例

        //如果我们强制增加一个hint提示,则用户id为newbie的文档未被返回,即走了索引(执行计划此处略)
        > db.scores.find().sort( { score: -1 } ).hint( { score: 1 } )
        { "_id" : ObjectId("523b6e6ffb408eea0eec2649"), "userid" : "nina", "score" : 90 }
        { "_id" : ObjectId("523b6e61fb408eea0eec2648"), "userid" : "abby", "score" : 82 }
    

    4、间隙索引与唯一约束

        在唯一索引中,唯一索引会把null当做值,也就是说为null的通常只能有一个。后面的null将无法插入。
        //下面创建一个带有唯一约束的稀疏索引
        > db.scores.createIndex( { score: 1 } , { sparse: true, unique: true } )
        {
                "ok" : 0,
                "errmsg" : "Index with name: score_1 already exists with different options",
                "code" : 85
        }
    
        //由于score列上已经存在一个索引了,因此提示我们,需要先删除,再创建
    
        > db.scores.dropIndex("score_1")
        { "nIndexesWas" : 2, "ok" : 1 }                  
    
        > db.scores.createIndex( { score: 1 } , { sparse: true, unique: true } )
        {
                "createdCollectionAutomatically" : false,
                "numIndexesBefore" : 1,
                "numIndexesAfter" : 2,
                "ok" : 1
        }
    
        //下面尝试插入一些带有score键以及不带有score键的文档,如下,可以成功插入
        > db.scores.insert( { "userid": "AAAAAAA", "score": 43 } )
        WriteResult({ "nInserted" : 1 })
        > db.scores.insert( { "userid": "CCCCCCC" } )
        WriteResult({ "nInserted" : 1 })
    
        //下面插入一些score相关的文档,提示重复,如下示例
        > db.scores.insert( { "userid": "AAAAAAA", "score": 82 } )
        WriteResult({
                "nInserted" : 0,
                "writeError" : {
                        "code" : 11000,
                        "errmsg" : "E11000 duplicate key error collection: test.scores index: score_1 dup key: { : 82.0 }"
                }
        })
        > db.scores.insert( { "userid": "BBBBBBB", "score": 90 } )
        WriteResult({
                "nInserted" : 0,
                "writeError" : {
                        "code" : 11000,
                        "errmsg" : "E11000 duplicate key error collection: test.scores index: score_1 dup key: { : 90.0 }"
                }
        })
    

    三、小结

    a、间隙索引就是创建索引的索引列在某些文档上列不存在,导致索引存在间隙。 
    b、间隙索引在创建时应指定选项:{ sparse: true } 
    c、间隙索引列上可以指定唯一性约束

    参见:http://blog.csdn.net/leshami/article/details/53897157

  • 相关阅读:
    Java实现 LeetCode 802 找到最终的安全状态 (DFS)
    Java实现 LeetCode 802 找到最终的安全状态 (DFS)
    Java实现 LeetCode 802 找到最终的安全状态 (DFS)
    Java实现 LeetCode 804 唯一摩尔斯密码词 (暴力)
    Java实现 LeetCode 803 打砖块 (DFS)
    Java实现 LeetCode 804 唯一摩尔斯密码词 (暴力)
    Java实现 LeetCode 803 打砖块 (DFS)
    Java实现 LeetCode 804 唯一摩尔斯密码词 (暴力)
    英文标点
    post sharp 与log4net 结合使用,含执行源码 转拷
  • 原文地址:https://www.cnblogs.com/GtShare/p/7661106.html
Copyright © 2011-2022 走看看