MongoDB for node.js developers 七周学习-最终测试小结(2)
2015-07-27 14:40
776 查看
还剩下一半:6、添加优化 7、游标 8、副本 9、片键 10、explain方法
6、添加优化
怎么加快insert的速度呢?
可以清除掉所有的index,因为有index再插入后MongoDB要维持index需要额外的开销
也可以修改写入关注(write concern),默认是W: 1,J: 0。
因为减少了等待时间,因此写入的速度也加快了,不过这是以降低安全性为代价的。
7、游标
游标本身提供了很多方法。
Cursor methods
.toArray() - iterates through docs and returns an array of the results
.forEach( func )
.map( func )
.hasNext()
.next()
.objsLeftInBatch() - returns count of docs left in current batch (when exhausted, a new getMore will be issued)
.itcount() - iterates through documents and counts them
.getQueryPlan() - get query plans associated with shape. To get more info on query plans, call getQueryPlan().help().
.pretty() - pretty print each document, possibly over multiple lines
我们可以结合.hasNext()与.next()进行迭代。
8、副本(replication)
副本是为了是数据更有可用性(availability),有一个主服务器处理客户端的请求,有多个备份服务器用于保存主服务器的数据副本。当主服务器崩溃后,备份服务器会自动选举一个成为主服务器。
当主服务器崩溃时,有些数据还来不及同步到备份服务器上,当这台服务器恢复后会比较现在主服务器的操作日志,然后进行回滚,将崩溃之前未复制的操作撤销,然后才能进行同步。
9、片键
分片是为了提供更大的负载,将原本的数据拆分到多个服务器上的方式。
片键则是将请求映射到服务器的关系,类似索引概念。
片键选择的时候要尽量避免大量的数据处理集中到一个服务器上,像以"_id"作为片键时,插入新的数据都会在最后一个服务器上,造成局部过热,这违背我们希望分散处理的初衷。
10、explain方法
syntax:db.collection.find().explain() or db.collection.explain() .find()
提供三种模式: "queryPlanner","executionStats", and "allPlansExecution"。信息提供是递进式的,也就是后面提供的更多。
6、添加优化
怎么加快insert的速度呢?
可以清除掉所有的index,因为有index再插入后MongoDB要维持index需要额外的开销
db.collection.dropIndex(index) //index TYPE string or document
也可以修改写入关注(write concern),默认是W: 1,J: 0。
db.messages.insert({_id: 1}); //先插入一个id为1的文档 WriteResult({ "nInserted" : 1 }) db.messages.insert({_id: 1},{ writeConcern: { w: 1 }}); //默认设置下w=1,所以其实跟上面一个命令一个意思,但不能有id一样的文档,所以返回错误信息 WriteResult({ "nInserted" : 0, "writeError" : { "code" : 11000, "errmsg" : "E11000 duplicate key error index: enron.messages.$_id_ dup key: { : 1.0 }" } }) db.messages.insert({_id: 1},{ writeConcern: { w: 0 }}); //把w设为0,也就是不等待写入返回结果,所以返回的是个空。 WriteResult({ });
因为减少了等待时间,因此写入的速度也加快了,不过这是以降低安全性为代价的。
7、游标
游标本身提供了很多方法。
Cursor methods
.toArray() - iterates through docs and returns an array of the results
.forEach( func )
.map( func )
.hasNext()
.next()
.objsLeftInBatch() - returns count of docs left in current batch (when exhausted, a new getMore will be issued)
.itcount() - iterates through documents and counts them
.getQueryPlan() - get query plans associated with shape. To get more info on query plans, call getQueryPlan().help().
.pretty() - pretty print each document, possibly over multiple lines
我们可以结合.hasNext()与.next()进行迭代。
var myCursor = db.inventory.find( { type: 'food' } ); while (myCursor.hasNext()) { print(tojson(myCursor.next())); }
8、副本(replication)
副本是为了是数据更有可用性(availability),有一个主服务器处理客户端的请求,有多个备份服务器用于保存主服务器的数据副本。当主服务器崩溃后,备份服务器会自动选举一个成为主服务器。
当主服务器崩溃时,有些数据还来不及同步到备份服务器上,当这台服务器恢复后会比较现在主服务器的操作日志,然后进行回滚,将崩溃之前未复制的操作撤销,然后才能进行同步。
9、片键
分片是为了提供更大的负载,将原本的数据拆分到多个服务器上的方式。
片键则是将请求映射到服务器的关系,类似索引概念。
片键选择的时候要尽量避免大量的数据处理集中到一个服务器上,像以"_id"作为片键时,插入新的数据都会在最后一个服务器上,造成局部过热,这违背我们希望分散处理的初衷。
10、explain方法
syntax:db.collection.find().explain() or db.collection.explain() .find()
提供三种模式: "queryPlanner","executionStats", and "allPlansExecution"。信息提供是递进式的,也就是后面提供的更多。
{ "queryPlanner" : { "plannerVersion" : <int>, "namespace" : <string>, "indexFilterSet" : <boolean>, "parsedQuery" : { ... }, "winningPlan" : { "stage" : <STAGE1>, ... "inputStage" : { "stage" : <STAGE2>, ... "inputStage" : { ... } } }, "rejectedPlans" : [ <candidate plan 1>, ... ] }
相关文章推荐
- mongodb启动失败汇总
- MongoDB:too much data for sort() with no index error异常
- MongoDB 宕机案例
- MongoDB学习笔记(六) MongoDB索引用法和效率分析
- MongoDB学习笔记(三) 在MVC模式下通过Jqgrid表格操作MongoDB数据
- MongoDB学习笔记(五) MongoDB文件存取操作
- MongoDB学习笔记(四) 用MongoDB的文档结构描述数据关系
- MongoDB学习笔记(二) 通过samus驱动实现基本数据操作
- MongoDB学习笔记(一) MongoDB介绍及安装
- 【MongoDB】在windows平台mongodb切片集群(三)
- MongoDB
- Win7下MongoDB的安装和部署测试
- windows安装 MongoDB 3.0.4
- golang中使用mongodb
- 第一节 MongoDB介绍及下载与安装
- MongoDB(4)--MongoDB服务的启动
- MongoDB常用操作命令大全
- tornado+ansible+twisted+mongodb操作和维护系统开发自己主动(一个)
- java连接MongoDB数据库
- Spring Data MongoDB 二:添加、删除操作