zoukankan      html  css  js  c++  java
  • MongoDB整理笔记のCapped Collection

    1、简单介绍

        capped collections 是性能出色的有着固定大小的集合,以LRU(Least Recently Used 最近最少使用)规则和插入顺序进行age-out(老化移出)处理,自动维护集合中对象的插入顺序,在创建时要预先指定大小。如果空间用完,新添加的对象将会取代集合中最旧的对象。

    2、功能特点

        可以插入及更新,但更新不能超出collection 的大小,否则更新失败。不允许删除,但是可以调用drop() 删除集合中的所有行,但是drop 后需要显式地重建集合。在32 位机上,一个capped collection 的最大值约为482.5M,64 位上只受系统文件大小的限制。

    3、常见用处
       (1)logging

        MongoDB 中日志机制的首选,MongoDB 没有使用日志文件,而是把日志事件存储在数据库中。在一个没有索引的capped collection 中插入对象的速度与在文件系统中记录日志的速度相当。

       (2)cache

        缓存一些对象在数据库中,比如计算出来的统计信息。这样的需要在collection 上建立一个索引,因为使用缓存往往是读比写多。

       (3)auto archiving

        可以利用capped collection 的age-out 特性,省去了写cron 脚本进行人工归档的工作。

    4、推荐用法
       (1)为了发挥capped collection 的最大性能,如果写比读多,最好不要在上面建索引,否则插入速度从"log speed"降为"database speed"。

       (2)使用"nature ordering"可以有效地检索最近插入的元素,因为capped collection 能够保证自然排序就是插入时的顺序,类似于log 文件上的tail 操作。

    5、注意事项

       (1)可以在创建capped collection 时指定collection 中能够存放的最大文档数。但这时也要指定size,因为总是先检查size 后检查maxRowNumber。可以使用validate()查看一个collection已经使用了多少空间,从而决定size 设为多大。如:
        db.createCollection("mycoll", {capped:true, size:100000, max:100});

        db.mycoll.validate();max=1 时会往collection 中存放尽量多的documents。

       (2)上述的createCollection 函数也可以用来创建一般的collection , 还有一个参数"autoIndexID",值可以为"true"和"false"来决定是否需要在"_id"字段上自动创建索引,如:
        db.createCollection("mycoll", {size:10000000, autoIndexId:false})。

        默认情况下对一般的collection 是创建索引的,但不会对capped collection 创建。

    6、案例操作

        MongoDB 支持 Capped Collection,一种固定大小的集合,当集合的大小达到指定大小时,新数据覆盖老数据,MongoDB Replica set 中的 oplog 就是 Capped Collection 类型。
       --1 查看 oplog 是否是 Capped Collection

     [mongo@redhatB ~]$ mongo 127.0.0.1:27018
       MongoDB shell version: 2.2.1
       connecting to: 127.0.0.1:27018/test
       rs0:PRIMARY> use local;
       switched to db local
       rs0:PRIMARY> show collections;
       me
       oplog.rs
       replset.minvalid
       slaves
       system.indexes
       system.replset
       rs0:PRIMARY> db.oplog.rs.isCapped();
       true

       备注:通过 db.collection.isCapped() 命令可以查看一个集合是否是 Capped Collection 。
       Capped Collection 具有以下特性,在使用的时候需要注意: 
       1 不可以对 Capped Collection 进行分片。
       2 在 2.2 版本以后,创建的Capped Collection 默认在  _id 字段上创建索引,而在 2.2 版本或以前没有。 
       3 在 Capped Collection 插入文档后可以进行更新(update)操作,当更新不能导致原来文档占用
           空间增长,否则更新失败。     
       4 不可以对 capped collection 执行删除文档操作,但可以删除整个集合。   
       接下来会测试其中的部分特性。 

       --2 创建 Capped Collection

      rs0:PRIMARY> db.createCollection("mycoll1",{capped:true,size:1024});  
      { "ok" : 1 }

       备注:通过 db.createCollection 命令创建 Capped Collection 集合,创建时必须指定集合大小,用于预先分配空间。

       --3 查看一个集合是否是 Capped Collection 
       可以通过以下两种方法查看一个集合是否是 Capped Collection 。

     rs0:PRIMARY> db.mycoll1.isCapped();
       true
       rs0:PRIMARY> db.mycoll1.stats();
       {
            "ns" : "test.mycoll1",
            "count" : 0,
            "size" : 0,
            "storageSize" : 4096,
            "numExtents" : 1,
            "nindexes" : 1,
            "lastExtentSize" : 4096,
            "paddingFactor" : 1,
            "systemFlags" : 1,
            "userFlags" : 0,
            "totalIndexSize" : 8176,
            "indexSizes" : {
                    "_id_" : 8176
            },
            "capped" : true,
            "max" : 2147483647,
            "ok" : 1
        }

       备注:"capped" 属性为 true 表示是 Capped Collection 。

       --4 测试:插入记录

    rs0:PRIMARY>  for (var i = 1; i <= 10000; i++) db.mycoll1.save({id : i, name : 'francs'});
       rs0:PRIMARY> db.mycoll1.find().count();
       56
       rs0:PRIMARY> db.mycoll1.find();
       { "_id" : ObjectId("50b811cf68b1911e7096db7f"), "id" : 9945, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db80"), "id" : 9946, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db81"), "id" : 9947, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db82"), "id" : 9948, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db83"), "id" : 9949, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db84"), "id" : 9950, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db85"), "id" : 9951, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db86"), "id" : 9952, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db87"), "id" : 9953, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db88"), "id" : 9954, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db89"), "id" : 9955, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db8a"), "id" : 9956, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db8b"), "id" : 9957, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db8c"), "id" : 9958, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db8d"), "id" : 9959, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db8e"), "id" : 9960, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db8f"), "id" : 9961, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db90"), "id" : 9962, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db91"), "id" : 9963, "name" : "francs" }
       { "_id" : ObjectId("50b811cf68b1911e7096db92"), "id" : 9964, "name" : "francs" }
       Type "it" for more

       备注:由于限制了集合大小不小,目标插入 10000 条,结果只插入了 56 条数据,并且老数据被新数据覆盖。另外不可以删除 Capped Collection 的文档,下面测试下。

       --5  测试: 删除 capped collection 中的文档

    rs0:PRIMARY> db.mycoll1.remove({id:9956});
       canot remove from a capped collection

       备注:删除文档时抛出异常。

       --6  测试:更新 capped collection 中的文档

    rs0:PRIMARY> db.mycoll1.find({id:9956});
       { "_id" : ObjectId("50b811cf68b1911e7096db8a"), "id" : 9956, "name" : "francs" }
       rs0:PRIMARY> db.mycoll1.update({id:9956},{$set:{name:'aaa_francs'}});
       failing update: objects in a capped ns cannot grow
       rs0:PRIMARY> db.mycoll1.update({id:9956},{$set:{name:'bbb'}});
       rs0:PRIMARY> db.mycoll1.find({id:9956});
       { "_id" : ObjectId("50b811cf68b1911e7096db8a"), "id" : 9956, "name" : "bbb" }

       备注:这里正好验证了特性3,更新后的值不能超过原有空间,否则更新失败。

       --7 官网参考
       http://docs.mongodb.org/manual/core/capped-collections/

     

  • 相关阅读:
    jQueryfocus,title,振动
    使用jQuery自动缩图片 (转载)
    jQuery10个小例子(jquery之旅).
    jQuery动态增加删除Tabs
    jQuery图片播放轮换
    jQuery插件上传控件美化
    Ajax简单
    jQuery仿QQ改版后的样式切换
    jQuery插件tooltip(超链接提示,图片提示).
    css分页样式
  • 原文地址:https://www.cnblogs.com/tomcatx/p/4245547.html
Copyright © 2011-2022 走看看