標籤:
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
[[email protected] ~]$ 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/
MongoDB整理筆記のCapped Collection