免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 2361 | 回复: 0
打印 上一主题 下一主题

[MongoDB] [原创] MongoDB管理与开发精要《红丸出品》16 索引 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-08-01 23:32 |只看该作者 |倒序浏览
第十六章 索引

MongoDB提供了多样性的索引支持,索引信息被保存在system.indexes中,且默认总是为_id创建索引,它的索引使用基本和MySQL等关系型数据库一样。其实可以这样说说,索引是凌驾于数据存储系统之上的另一层系统,所以各种结构迥异的存储都有相同或相似的索引实现及使用接口并不足为奇。

16.1 基础索引

在字段age上创建索引,1(升序);-1(降序)

> db.t3.ensureIndex({age:1})
> db.t3.getIndexes();
[
        {
                "name" : "_id_",
                "ns" : "test.t3",
                "key" : {
                        "_id" : 1
                },
                "v" : 0
        },
        {
                "_id" : ObjectId("4fb906da0be632163d0839fe"),
                "ns" : "test.t3",
                "key" : {
                        "age" : 1
                },
                "name" : "age_1",
                "v" : 0
        }
]
>

上例显示出来的一共有2个索引,其中_id是创建表的时候自动创建的索引,此索引是不能够删除的。


当系统已有大量数据时,创建索引就是个非常耗时的活,我们可以在后台执行,只需指定“backgroud:true”即可。

> db.t3.ensureIndex({age:1} , {backgroud:true})
16.2 文档索引

索引可以任何类型的字段,甚至文档

db.factories.insert( { name: "wwl", addr: { city: "Beijing", state: "BJ" } } );

//在addr列上创建索引
db.factories.ensureIndex( { addr : 1 } );

//下面这个查询将会用到我们刚刚建立的索引
db.factories.find( { addr: { city: "Beijing", state: "BJ" } } );

//但是下面这个查询将不会用到索引,因为查询的顺序跟索引建立的顺序不一样
db.factories.find( { addr: { state: "BJ" , city: "Beijing"} } );
16.3 组合索引

跟其它数据库产品一样,MongoDB也是有组合索引的,下面我们将在addr.city和addr.state上建立组合索引。当创建组合索引时,字段后面的1表示升序,-1表示降序,是用1还是用-1主要是跟排序的时候或指定范围内查询的时候有关的。

db.factories.ensureIndex( { "addr.city" : 1, "addr.state" : 1 } );

// 下面的查询都用到了这个索引
db.factories.find( { "addr.city" : "Beijing", "addr.state" : "BJ" } );
db.factories.find( { "addr.city" : "Beijing" } );
db.factories.find().sort( { "addr.city" : 1, "addr.state" : 1 } );
db.factories.find().sort( { "addr.city" : 1 } )
16.4 唯一索引

只需在ensureIndex命令中指定”unique:true”即可创建唯一索引。例如,往表t4中插入2条记录

db.t4.insert({firstname: "wang", lastname: "wenlong"});
db.t4.insert({firstname: "wang", lastname: "wenlong"});

在t4表中建立唯一索引

> db.t4.ensureIndex({firstname: 1, lastname: 1}, {unique: true});
E11000 duplicate key error index: test.t4.$firstname_1_lastname_1  dup key: { : "wang", : "wenlong" }

可以看到,当建唯一索引时,系统报了“表里有重复值”的错,具体原因就是因为表中有2条一模一模的数据,所以建立不了唯一索引。

16.5 强制使用索引

hint命令可以强制使用某个索引。

> db.t5.insert({name: "wangwenlong",age: 20})
> db.t5.ensureIndex({name:1, age:1})
> db.t5.find({age:{$lt:30}}).explain()
{
        "cursor" : "BasicCursor",
        "nscanned" : 1,
        "nscannedObjects" : 1,
        "n" : 1,
        "millis" : 0,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "isMultiKey" : false,
        "indexOnly" : false,
        "indexBounds" : {               --并没有用到索引

        }
}
> db.t5.find({age:{$lt:30}}).hint({name:1, age:1}).explain()      --强制使用索引
{
        "cursor" : "BtreeCursor name_1_age_1",
        "nscanned" : 1,
        "nscannedObjects" : 1,
        "n" : 1,
        "millis" : 1,
        "nYields" : 0,
        "nChunkSkips" : 0,
        "isMultiKey" : false,
        "indexOnly" : false,
        "indexBounds" : {                --被强制使用索引了
                "name" : [
                        [
                                {
                                        "$minElement" : 1
                                },
                                {
                                        "$maxElement" : 1
                                }
                        ]
                ],
                "age" : [
                        [
                                -1.7976931348623157e+308,
                                30
                        ]
                ]
        }
}
>
16.6 删除索引

删除索引分为删除某张表的所有索引和删除某张表的某个索引,具体如下:

//删除t3表中的所有索引
db.t3.dropIndexes()

//删除t4表中的firstname索引
db.t4.dropIndex({firstname: 1})







  • [url=]本版精华[/url]






大学生DBA训练营(ChinaDBA.net)创始人!学费1元,也就是这1元钱的力量,足以改变你的职业生涯
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP