Mongodb ç´¹»‹

  • View
    938

  • Download
    0

Embed Size (px)

Text of Mongodb ç´¹»‹

  • 1. MongoDB2012/5/18 matsumura

2. MongoDB - but o 2.0.5 o Read / Write o Master dead o SQLJOIN o 3. WebWebmongos mongos3process processdata data metamongod mongod Mongod(config) mongod mongodMongod(config)mongod mongodMongod ReplicaReplica (config) setset 4. MySQL mongod mysqld etcr XX doc doc doc doc doc doc doc doc doc doc other doc doc doc 5. - MySQLMongod (Primary) Mongod(Secondary)Mongod(Secondary) etcr etcr etcr docA docB docC docA docB docC docA docB docCdocD docE docF docD docE docF docD docE docF local local local oplog oplog oplog 6. Primary Primaryreplica set Primary 20s 60s 7. - phase1Mongod (Shard A)Mongod (Shard B) etcr etcr Chunk ( ]docA docB docC 200MB 8. - phase2Mongod (Shard A)Mongod (Shard B) etcr etcr Chunk ( D] docA docB docCChunk ( D ]docDdocE docF 9. - phase3Mongod (Shard A)Mongod (Shard B) etcr etcr Chunk ( D] docA docB docCdocCdocC docCChunk ( D ]docDdocE docF 10. - phase4Mongod (Shard A)Mongod (Shard B) etcr etcr Chunk ( C] docA docB docCChunk (C D]docCdocC docCChunk ( D ] docD docE docF 11. - phase5Mongod (Shard A)Mongod (Shard B) etcr etcr Chunk ( C]Chunk ( D ] docA docB docC docD docE docFChunk (C D]docCdocC docC 12. - phase6Sharding Demo 13. Create table, Create column family o Inserto Item1 = {_id: ObjectId(4b0552b0f0da7d1eb6f12xxx),name: ,price: 100,}Item2 = {_id: ObjectId(4b0552b0f0da7d1eb6f12yyy),name: ,} 14. SQLDemoO/R o javascript 15. (1)Index(B-Tree)o 1 / indexo int // index db.test.ensureIndex({x:1, y:1, z:1}) db.test.find({x:a}) db.test.find({x:a, y:b}) db.test.find({x:a, y:b}).sort({z:1}) // db.test.find({y:b, x:a}) 16. (2) oMySQL o o o o explain() 17. (3)Capped o o o o Sharding o o Create db.createCollection("mycoll", {capped:true,size:100000}) 18. find (1) bson1. db.activityHistoryDemo.find({owner_id: 123})2. db.activityHistoryDemo.find({concerned.id: 201}){owner_id: 123,request: { type: , params: {}},process: {category: composit},memo: [lv.0 -> lv.15],concerned: [{io: i, type: card, id: 100, object: 111, base: true},{io: i, type: card, id: 201, object: 222},{io: o, type: card, id: 100, object: 111},]}) 19. find (2) bsondb.activityHistoryDemo.find({concerned.id: 201}) { owner_id: 123, request: { type: , params: {}}, process: {category: composit}, memo: [lv.0 -> lv.15], concerned: [ {io: i, type: card, id: 100, object: 111, base: true}, {io: i, type: card, id: 201, object: 222}, {io: o, type: card, id: 100, object: 111}, ] }) 20. find (3) o And db.sample.find({owner_id: 123, concerned.type: card}) db.sample.find({concerned.type: card, owner_id: 123}) o Or db.sample.find({$or: [{concerned.type: card}, {owner_id: 123}]) db.sample.find({$or: [{owner_id: 123}, {concerned.type: card}]) 21. find (4) DBRefdoc = {name: ryooo,card:[{$ref: card, $id : ObjectId(4b0552b0f0da7d1eb6f12xxx)},]}doc.card[0].fetch() // @rubydb = Connection.new.db(etcr ")user_card = db["user_card"].save({:name => ryooo, :card_id => 123})ref = DBRef.new(card", user_card.card_id)db.dereference(ref)#=> 22. find (5) (javascript) // db.cards.find(function(){ row = db.user_summary.findOne({owner_id: this.owner_id}) return this._id == row.leader_card_id; }) mongo // db.system.js.save({_id:name, value: function (){ //implementation }}); f = db.system.js.findOne({_id:name}) // (f) Db.cards.find(f) 23. find (6) Shard key Shard keytargeted globalMongod (Shard A) Mongod (Shard B) etcr etcr ChunkChunk docA docB docC docD docE docF 24. insert/update (1) fire and forget o return getlasterror commit o fsync: true o 2(timeout:5000) db.getlasterror(2, 5000) db.getlasterror(majority) 25. insert/update (2) ID o ID o ObjectId = BSON( [4byte timestamp] + [3byte hash(hostname)] + [2byte pid] + [3byte inc]) // parse object_id = 4b0552b0f0da7d1eb6f12yyy createdDt = new Date(parseInt(object_id.substr(0, 8), 16) * 1000) #=> Thu Nov 19 2009 23:14:08 GMT+0900 (JST) 26. insert/update (3) Paddingo insert update(In-place update)o Padding () Padding Atomico 1o ACIDA(atomic : all or nothing)o sharding 27. remove o o repair (shardingshard)o compact paddingupdate 28. - 1 flush o 60s1flush 60 o 1 (ver1.8) 60 getlasterror()flush o 2 (ver1.8) (disc)100ms1 flush Journal 29. - 2 (flush)DB o CPU o MapReduce o 30. Shard key o Chunko o Shard key Shard key Migration 31. Shard key () [bad] o chunk [bad] o chunk [bad] o chunk [good] o yyyymm-owner_id 32. PFWeb WebMap/ReduceI/Ojson