设为首页 收藏本站
查看: 811|回复: 0

[经验分享] mongodb(六):索引、执行计划及地理空间索引

[复制链接]
累计签到:1 天
连续签到:1 天
发表于 2014-9-2 09:21:34 | 显示全部楼层 |阅读模式
                                索引是用来加速查询的
        准备数据
                                                                        >  db.users.insert({"username":"smith","age":48,"user_id":0})                               
                                                                        >  db.users.insert({"username":"smith","age":30,"user_id":1})                                
                                                                        >  db.users.insert({"username":"john","age":36,"user_id":2})                                
                                                                        >  db.users.insert({"username":"john","age":18,"user_id":3})                               
                                                                        >  db.users.insert({"username":"joe","age":36,"user_id":4})                                
                                                                        >  db.users.insert({"username":"john","age":7,"user_id":5})                               
                                                                        > db.users.insert({"username":"simon","age":3,"user_id":6})                               
                                                                        >  db.users.insert({"username":"joe","age":27,"user_id":7})                                 
                                                                        >  db.users.insert({"username":"jacob","age":17,"user_id":8})                               
                                                                        >  db.users.insert({"username":"sally","age":52,"user_id":9})                               
                                                                        >  db.users.insert({"username":"simon","age":59,"user_id":10})                               
                       
         
        .单键索引
        1.在没有创建索引前的执行计划(explain)
                                                                        >  db.users.find({"username":"joe"}).explain()                               
                                                                        {                               
                                                                                 "cursor"  : "BasicCursor",(cursorBasicCursor说明该查询没有走上索引)                               
                                                                                 "nscanned"  : 11,(查询的时候浏览了11个文档)                               
                                                                                 "nscannedObjects"  : 11,                               
                                                                                 "n"  : 2,(返回文档的数量)                               
                                                                                 "millis"  : 0,(执行查询时间,这里的文档的username重复很高,所以看不出效果,在这里只给出测试方法)                               
                                                                                 "indexBounds"  : {                               
                                                                                                                          
                                                                                 }                               
                                                                        }                               
                       
         
        2.创建索引和查看索引
                                                                        >  db.users.ensureIndex({"username":1},{"name":"index_username"})                               
                                                                        > db.users.getIndexes()                               
                                                                        [                               
                                                                                 {                               
                                                                                           "name"  : "_id_",                               
                                                                                           "ns"  : "test.users",                               
                                                                                           "key"  : {                               
                                                                                                    "_id"  : 1                               
                                                                                           }                               
                                                                                 },                               
                                                                                 {                               
                                                                                           "_id"  : ObjectId("53f9ec055724be10470e0f42"),                               
                                                                                           "ns"  : "test.users",                               
                                                                                           "key"  : {                               
                                                                                                    "username"  : 1                               
                                                                                           },                               
                                                                                           "name"  : "index_username"                               
                                                                                 }                               
                                                                        ]                               
                                                                                                        
                                                                        > db.system.indexes.find()                               
                                                                        { "name" : "_id_",  "ns" : "test.blog", "key" : { "_id" :  1 } }                               
                                                                        { "name" : "_id_",  "ns" : "test.foo", "key" : { "_id" :  1 } }                               
                                                                        { "name" : "_id_",  "ns" : "test.c", "key" : { "_id" : 1  } }                               
                                                                        { "name" : "_id_",  "ns" : "test.users", "key" : { "_id"  : 1 } }                               
                                                                        { "_id" :  ObjectId("53f9ec055724be10470e0f42"), "ns" :  "test.users", "key" : { "username" : 1 },  "name" : "index_username" }                               
                       
         
        3.在创建索引后的执行计划
                                                                        >  db.users.find({"username":"joe"}).explain()                                                  
                                                                        {                               
                                                                                 "cursor"  : "BtreeCursor index_username",(这里走上了索引)                               
                                                                                 "nscanned"  : 2,                               
                                                                                 "nscannedObjects"  : 2,                               
                                                                                 "n"  : 2,                               
                                                                                 "millis"  : 0,                               
                                                                                 "indexBounds"  : {                               
                                                                                           "username"  : [                               
                                                                                                    [                               
                                                                                                             "joe",                               
                                                                                                             "joe"                               
                                                                                                    ]                               
                                                                                           ]                               
                                                                                 }                               
                                                                        }                               
                       
         
        4.删除索引,先查在删
                                                                        >  db.runCommand({"dropIndexes": "users", "index":  "index_username"})                               
                                                                        { "nIndexesWas" : 2,  "ok" : 1 }                               
                       
         
        .复合索引(多键)
        1.创建复合索引
                                                                        >  db.users.ensureIndex({"username":1,"age":1})                                
                                                                        > db.system.indexes.find()                               
                                                                        { "name" : "_id_",  "ns" : "test.blog", "key" : { "_id" :  1 } }                               
                                                                        { "name" : "_id_",  "ns" : "test.foo", "key" : { "_id" :  1 } }                               
                                                                        { "name" : "_id_",  "ns" : "test.c", "key" : { "_id" : 1  } }                               
                                                                        { "name" : "_id_",  "ns" : "test.users", "key" : { "_id"  : 1 } }                               
                                                                        { "_id" :  ObjectId("53f9f62c5724be10470e0f45"), "ns" :  "test.users", "key" : { "username" : 1,  "age" : 1 }, "name" : "username_1_age_1" }                               
                                                                                                        
                                                                        >  db.users.ensureIndex({"age":1,"username":1})                                      
                                                                        > db.system.indexes.find()                                                          
                                                                        { "name" : "_id_",  "ns" : "test.blog", "key" : { "_id" :  1 } }                               
                                                                        { "name" : "_id_",  "ns" : "test.foo", "key" : { "_id" :  1 } }                               
                                                                        { "name" : "_id_",  "ns" : "test.c", "key" : { "_id" : 1  } }                               
                                                                        { "name" : "_id_",  "ns" : "test.users", "key" : { "_id"  : 1 } }                               
                                                                        { "_id" :  ObjectId("53f9f62c5724be10470e0f45"), "ns" :  "test.users", "key" : { "username" : 1,  "age" : 1 }, "name" : "username_1_age_1" }                               
                                                                        { "_id" :  ObjectId("53f9f7ea5724be10470e0f46"), "ns" :  "test.users", "key" : { "age" : 1,  "username" : 1 }, "name" : "age_1_username_1" }                               
                       
         
        上面有两个索引username_1_age_1age_1_username_1
         
        2.在来看查询的执行计划
                                                                        >  db.users.find({"age":18,"username":/.*/}).explain()                               
                                                                        {                               
                                                                                 "cursor"  : "BtreeCursor age_1_username_1 multi",                               
                                                                                 "nscanned"  : 1,                               
                                                                                 "nscannedObjects"  : 1,                               
                                                                                 "n"  : 1,                               
                                                                                 "millis"  : 0,                               
                                                                                 "indexBounds"  : {                               
                                                                                           "age"  : [                               
                                                                                                    [                               
                                                                                                             18,                               
                                                                                                             18                               
                                                                                                    ]                               
                                                                                           ],                               
                                                                                           "username"  : [                               
                                                                                                    [                               
                                                                                                             "",                               
                                                                                                             {                               
                                                                                                                                                      
                                                                                                             }                               
                                                                                                    ],                               
                                                                                                    [                               
                                                                                                             /.*/,                               
                                                                                                             /.*/                               
                                                                                                    ]                               
                                                                                           ]                               
                                                                                 }                               
                       
        这里走上了age_1_username_1
        我们希望走上的是username_1_age_1
        所以应该强制让它走上username_1_age_1(hint)
        这里和oracle中对CBO调优类似,让查询按照我们想要的索引走,而不是根据优化器走
        (小记:oracle中的调优技术:nestloop,hash join,merge join根据不同的需求进行选择)
                                                                        >  db.users.find({"age":18,"username":/.*/}).hint({"username":1,"age":1}).explain()                               
                                                                        {                               
                                                                                 "cursor"  : "BtreeCursor username_1_age_1 multi",                               
                                                                                 "nscanned"  : 1,                               
                                                                                 "nscannedObjects"  : 1,                               
                                                                                 "n"  : 1,                               
                                                                                 "millis"  : 0,                               
                                                                                 "indexBounds"  : {                               
                                                                                           "username"  : [                               
                                                                                                    [                               
                                                                                                             "",                               
                                                                                                             {                               
                                                                                                                                                      
                                                                                                             }                               
                                                                                                    ],                               
                                                                                                    [                               
                                                                                                             /.*/,                               
                                                                                                             /.*/                               
                                                                                                    ]                               
                                                                                           ],                               
                                                                                           "age"  : [                               
                                                                                                    [                               
                                                                                                             18,                               
                                                                                                             18                               
                                                                                                    ]                               
                                                                                           ]                               
                                                                                 }                               
                                                                        }                               
                       
         
        3.修改索引
                                                                        > db.users.ensureIndex({"age":1,"username":1},{"background":true})                               
                                                                        作用索引的老化,不得不重新修改                               
                       
         
        .地理空间索引
        Mongodb为坐标平面查询用的
        建立地理空间索引的键的值必须是一对值:一个包含两个数值的数组或包含两个键的内嵌文档(内嵌文档的键的名称无所谓),如:{gps”:[0100]}{gps”:{x”:-30,“y”:30}}{gps”:{latitude”:-180,“longitude”:180}}
         
        准备数据
                                                                        >  db.map.insert({"location":[50,100],"desc":"coffee"})                                                                                          
                                                                        >  db.map.insert({"location":[-70,30],"desc":"coffeeshop"})                                             
                                                                        >  db.map.insert({"location":[100,150],"desc":"muffins"})                                  
                                                                        > db.map.insert({"location":[-90,-20],"desc":"espresso"})                               
                       
         
        1.创建索引
                                                                        >  db.map.ensureIndex({"location":"2d","desc":1})  这里用2d且要用双引号                               
                                                                        > db.map.getIndexes()                               
                                                                        [                               
                                                                                 {                               
                                                                                           "name"  : "_id_",                               
                                                                                           "ns"  : "test.map",                               
                                                                                           "key"  : {                               
                                                                                                    "_id"  : 1                               
                                                                                           }                               
                                                                                 },                               
                                                                                 {                               
                                                                                           "_id"  : ObjectId("53fa02be5724be10470e0f4c"),                               
                                                                                           "ns"  : "test.map",                               
                                                                                           "key"  : {                               
                                                                                                    "location"  : "2d",                               
                                                                                                    "desc"  : 1                               
                                                                                           },                               
                                                                                           "name"  : "location__desc_1"                               
                                                                                 }                               
                                                                        ]                               
                       
         
        2.查找最近的咖啡店用”$near"
                                                                        >  db.map.find({"location":{"$near":[-70,100]},"desc":"coffee"}).limit(1)                               
                                                                        { "_id" : ObjectId("53fa01625724be10470e0f48"),  "location" : [ 50, 100 ], "desc" : "coffee" }                               
                       
         
        3.在矩形里查找
                                                                        >  db.map.find({"location":{"$within":{"$box":[[-70,0],[50,150]]}},"desc":"coffeeshop"})                               
                                                                        { "_id" :  ObjectId("53fa01a65724be10470e0f49"), "location" : [ -70,  30 ], "desc" : "coffeeshop" }                               
                       
        通过{"$box":[[-70,0],[50,150]]}指定一个矩形,其左下角和右上角坐标;”$within”指定查询在这个范围内的点。
         
        4.在圆形区域查找
                                                                        >  db.map.find({"location":{"$within":{"$center":[[-70,0],50]}},"desc":"coffeeshop"})                               
                                                                        { "_id" :  ObjectId("53fa01a65724be10470e0f49"), "location" : [ -70,  30 ], "desc" : "coffeeshop" }                               
                       
        {"$center":[[-70,0],50]},指定了圆形的圆心坐标和半径; ”$within”指定查询在这个范围内的点。
         
           


运维网声明 1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com

所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其承担任何法律责任,如涉及侵犯版权等问题,请您及时通知我们,我们将立即处理,联系人Email:kefu@iyunv.com,QQ:1061981298 本贴地址:https://www.yunweiku.com/thread-24265-1-1.html 上篇帖子: mongodb(五):游标、高级查询及游标内幕 下篇帖子: mongodb 编译安装使用(Linux)
您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

扫码加入运维网微信交流群X

扫码加入运维网微信交流群

扫描二维码加入运维网微信交流群,最新一手资源尽在官方微信交流群!快快加入我们吧...

扫描微信二维码查看详情

客服E-mail:kefu@iyunv.com 客服QQ:1061981298


QQ群⑦:运维网交流群⑦ QQ群⑧:运维网交流群⑧ k8s群:运维网kubernetes交流群


提醒:禁止发布任何违反国家法律、法规的言论与图片等内容;本站内容均来自个人观点与网络等信息,非本站认同之观点.


本站大部分资源是网友从网上搜集分享而来,其版权均归原作者及其网站所有,我们尊重他人的合法权益,如有内容侵犯您的合法权益,请及时与我们联系进行核实删除!



合作伙伴: 青云cloud

快速回复 返回顶部 返回列表