mongodb有关的研究.docx
《mongodb有关的研究.docx》由会员分享,可在线阅读,更多相关《mongodb有关的研究.docx(38页珍藏版)》请在冰豆网上搜索。
mongodb有关的研究
mongodb有关的研究
mongodb是一款文档型的非关系型数据库,性能非常高,老赵做过相关测试,我测试下来也差不多,和sqlserver相比,写入性能好太多了,下面是我的测试结果:
一条记录4K,1000万的数据占50G磁盘包括索引
Documentdoc=newDocument();
doc.Add("Name","Joseph"+DateTime.Now.Second);索引
doc.Add("Age",r.Next(20,50));索引
doc.Add("Time",DateTime.Now);
doc.Add("Data",s);
记录很少的时候写吞吐5000左右
记录很少的时候基于索引的搜索每秒2000左右
数据很少的时候读吞吐每秒1400
数据很少的时候4M数据获取大概237毫秒
数据很少的时候搜索对写入影响不大
数据很少的时候读写相互影响不大
数据很多的时候
写入速度下降一半
读取速度没区别
搜索慢了很多,千万数据200毫秒的搜索速度,2个字段
读取对写入影响还是不大
查询对写入影响很大
另不管数据量多少CPU始终在10%以下,内存随着数据量增多从20%-80%
查询:
Documentdoc=newDocument();
doc.Add("Name","Joseph10");
doc.Add("Age",newDocument().Append("$gte",30).Append("$lte",40));
读取:
table.FindOne(newDocument());
测试程序点击这里下载。
测试基于单个master的mongodb,后续测试发现,如果开启master/slave的话,写入速度非常不稳定,在向slave传送数据的时候写入速度下降50%到80%。
还有就是mongodb随着并发的上升并没有带来写入速度上的提高,反而下降了一点,可能是因为读写锁的关系。
另外,点击这里下载一个查看mongodb对象的小工具(基于MongoDB.Driver.dll):
列出服务器
列出数据库表索引
服务器属性
数据库属性
表属性
列数据
通过写这个工具我觉得,Document使用树形来表示还是很方便的。
我还在想怎么通过类似sql语句的方式查询mongodb,找到了一个gudusoft.gsqlparser,可以解析各种sql,拿来一用,点击这里下载通过sql语句操作mongodb的测试工具,这个工具还是非常原始的版本,只能进行最简单的crud,select不支持grouphaving等子句:
其实把这些小工具组合在一起就是一个简单的mongodb客户管理工具。
在http:
//www.mongodb.org/display/DOCS/List+of+Database+Commands上作者列出了所有db的命令,我们可以通过提交command操作。
在http:
//www.mongodb.org/display/DOCS/DBA+Operations+from+the+Shell上作者列出了可以在mongod工具中做的一些操作,其实这些操作完全可以通过提交command来进行,因为mongod工具完全就是一个javascript写的,我们看一下源代码中的mongo_vstudio.cpp:
constchar*jsconcatcode=
"__quiet=false;\n"
"chatty=function(s){\n"
"if(!
__quiet)\n"
"print(s);}\n"
"friendlyEqual=function(a,b){\n"
"if(a==b)\n"
"returntrue;\n"
"if(tojson(a)==tojson(b))\n"
"returntrue;\n"
"returnfalse;}\n"
"doassert=function(msg){\n"
"print(\"assert:
\"+msg);\n"
"throwmsg;}\n"
"assert=function(b,msg){\n"
"if(assert._debug&&msg)print(\"inassertfor:
\"+msg);\n"
"if(b)\n"
"return;\n"
"doassert(\"assertfailed:
\"+msg);}\n"
"assert._debug=false;\n"
"assert.eq=function(a,b,msg){\n"
"if(assert._debug&&msg)print(\"inassertfor:
\"+msg);\n"
"if(a==b)\n"
"return;\n"
"if((a!
=null&&b!
=null)&&friendlyEqual(a,b))\n"
"return;\n"
"doassert(\"[\"+tojson(a)+\"]!
=[\"+tojson(b)+\"]arenotequal:
\"+msg);}\n"
"assert.neq=function(a,b,msg){\n"
"if(assert._debug&&msg)print(\"inassertfor:
\"+msg);\n"
"if(a!
=b)\n"
"return;\n"
"doassert(\"[\"+a+\"]!
=[\"+b+\"]areequal:
\"+msg);}\n"
"assert.soon=function(f,msg,timeout,interval){\n"
"if(assert._debug&&msg)print(\"inassertfor:
\"+msg);\n"
"varstart=newDate();\n"
"timeout=timeout||30000;\n"
"interval=interval||200;\n"
"varlast;\n"
"while
(1){\n"
"if(typeof(f)==\"string\"){\n"
"if(eval(f))\n"
"return;}\n"
"else{\n"
"if(f())\n"
"return;}\n"
"if((newDate()).getTime()-start.getTime()>timeout)\n"
"doassert(\"assert.soonfailed:
\"+f+\",msg:
\"+msg);\n"
"sleep(interval);}}\n"
"assert.throws=function(func,params,msg){\n"
"if(assert._debug&&msg)print(\"inassertfor:
\"+msg);\n"
"try{\n"
"func.apply(null,params);}\n"
"catch(e){\n"
"returne;}\n"
"doassert(\"didnotthrowexception:
\"+msg);}\n"
"mandWorked=function(res,msg){\n"
"if(assert._debug&&msg)print(\"inassertfor:
\"+msg);\n"
"if(res.ok==1)\n"
"return;\n"
"doassert(\"commandfailed:
\"+tojson(res)+\":
\"+msg);}\n"
"mandFailed=function(res,msg){\n"
"if(assert._debug&&msg)print(\"inassertfor:
\"+msg);\n"
"if(res.ok==0)\n"
"return;\n"
"doassert(\"commandworkedwhenitshouldhavefailed:
\"+tojson(res)+\":
\"+msg);}\n"
"assert.isnull=function(what,msg){\n"
"if(assert._debug&&msg)print(\"inassertfor:
\"+msg);\n"
"if(what==null)\n"
"return;\n"
"doassert(\"supposedtonull(\"+(msg||\"\")+\")was:
\"+tojson(what));}\n"
"assert.lt=function(a,b,msg){\n"
"if(assert._debug&&msg)print(\"inassertfor:
\"+msg);\n"
"if(a
"return;\n"
"doassert(a+\"isnotlessthan\"+b+\":
\"+msg);}\n"
"assert.gt=function(a,b,msg){\n"
"if(assert._debug&&msg)print(\"inassertfor:
\"+msg);\n"
"if(a>b)\n"
"return;\n"
"doassert(a+\"isnotgreaterthan\"+b+\":
\"+msg);}\n"
"assert.close=function(a,b,msg){\n"
"vardiff=Math.abs((a-b)/((a+b)/2));\n"
"if(diff<.001)\n"
"return;\n"
"doassert(a+\"isnotcloseto\"+b+\"diff:
\"+diff+\":
\"+msg);}\n"
"Object.extend=function(dst,src,deep){\n"
"for(varkinsrc){\n"
"varv=src[k];\n"
"if(deep&&typeof(v)==\"object\"){\n"
"v=Object.extend(typeof(v.length)==\"number\"?
[]:
{},v,true);}\n"
"dst[k]=v;}\n"
"returndst;}\n"
"argumentsToArray=function(a){\n"
"vararr=[];\n"
"for(vari=0;i"arr[i]=a[i];\n"
"returnarr;}\n"
"isString=function(x){\n"
"returntypeof(x)==\"string\";}\n"
"isNumber=function(x){\n"
"returntypeof(x)==\"number\";}\n"
"isObject=function(x){\n"
"returntypeof(x)==\"object\";}\n"
"String.prototype.trim=function(){\n"
"returnthis.replace(/^\\s+|\\s+$/g,\"\");}\n"
"String.prototype.ltrim=function(){\n"
"returnthis.replace(/^\\s+/,\"\");}\n"
"String.prototype.rtrim=function(){\n"
"returnthis.replace(/\\s+$/,\"\");}\n"
"Date.timeFunc=function(theFunc,numTimes){\n"
"varstart=newDate();\n"
"numTimes=numTimes||1;\n"
"for(vari=0;i"theFunc.apply(null,argumentsToArray(arguments).slice
(2));}\n"
"return(newDate()).getTime()-start.getTime();}\n"
"Date.prototype.tojson=function(){\n"
"return\"\\\"\"+this.toString()+\"\\\"\";}\n"
"RegExp.prototype.tojson=RegExp.prototype.toString;\n"
"Array.contains=function(a,x){\n"
"for(vari=0;i"if(a[i]==x)\n"
"returntrue;}\n"
"returnfalse;}\n"
"Array.unique=function(a){\n"
"varu=[];\n"
"for(vari=0;i"varo=a[i];\n"
"if(!
Array.contains(u,o)){\n"
"u.push(o);}}\n"
"returnu;}\n"
"Array.shuffle=function(arr){\n"
"for(vari=0;i"varpos=i+Random.randInt(arr.length-i);\n"
"varsave=arr[i];\n"
"arr[i]=arr[pos];\n"
"arr[pos]=save;}\n"
"returnarr;}\n"
"Array.tojson=function(a,indent){\n"
"if(!
indent)\n"
"indent=\"\";\n"
"if(a.length==0){\n"
"return\"[]\";}\n"
"vars=\"[\\n\";\n"
"indent+=\"\\t\";\n"
"for(vari=0;i"s+=indent+tojson(a[i],indent);\n"
"if(i"s+=\",\\n\";}}\n"
"if(a.length==0){\n"
"s+=indent;}\n"
"indent=indent.substring
(1);\n"
"s+=\"\\n\"+indent+\"]\";\n"
"returns;}\n"
"Array.fetchRefs=function(arr,coll){\n"
"varn=[];\n"
"for(vari=0;i"varz=arr[i];\n"
"if(coll&&coll!
=z.getCollection())\n"
"continue;\n"
"n.push(z.fetch());}\n"
"returnn;}\n"
"Array.sum=function(arr){\n"
"if(arr.length==0)\n"
"returnnull;\n"
"vars=arr[0];\n"
"for(vari=1;i"s+=arr[i];\n"
"returns;}\n"
"Array.avg=function(arr){\n"
"if(arr.length==0)\n"
"returnnull;\n"
"returnArray.sum(arr)/arr.length;}\n"
"Array.stdDev=function(arr){\n"
"varavg=Array.avg(arr);\n"
"varsum=0;\n"
"for(vari=0;i"sum+=Math.pow(arr[i]-avg,2);}\n"
"returnMath.sqrt(sum/arr.length);}\n"
"Object.keySet=function(o){\n"
"varret=newArray();\n"
"for(iino){\n"
"if(!
(iino.__proto__&&o[i]===o.__proto__[i])){\n"
"ret.push(i);}}\n"
"returnret;}\n"
"if(!
ObjectId.prototype)\n"
"ObjectId.prototype={}\n"
"ObjectId.prototype.toString=function(){\n"
"returnthis.str;}\n"
"ObjectId.prototype.tojson=function(){\n"
"return\"ObjectId(\\\"\"+this.str+\"\\\")\";}\n"
"ObjectId.prototype.isObjectId=true;\n"
"if(typeof(DBPointer)!
=\"undefined\"){\n"
"DBPointer.prototype.fetch=function(){\n"
"assert(this.ns,\"needans\");\n"
"assert(this.id,\"needanid\");\n"
"returndb[this.ns].findOne({_id:
this.id});}\n"
"DBPointer.prototype.tojson=function(indent){\n"
"returntojson({\"ns\":
this.ns,\"id\":
this.id},indent);}\n"
"DBPointer.prototype.getCollection=function(){\n"
"returnthis.ns;}\n"
"DBPointer.prototype.toString=function(){\n"
"return\"DBPointer\"+this.ns+\":
\"+this.id;}}\n"
"else{\n"
"print(\"warning:
noDBPointer\");}\n"
"if(typeof(DBRef)!
=\"undefined\"){\n"
"DBRef.prototype.fetch=function(){\n"
"assert(this.$ref,\"needans\");\n"
"assert(this.$id,\"needanid\");\n"
"returndb[this.$ref].findOne({_id:
this.$id});}\n"
"DBRef.prototype.tojson=function(indent){\n"
"returntojson({\"$ref\":
this.$ref,\"$id\":
this.$id},indent);}\n"
"DBRef.prototype.getCollection=function(){\n"
"returnthis.$ref;}\n"
"DBRef.prototype.toString=function(){\n"
"returnthis.tojson();}}\n"
"else{\n"
"print(\"warning:
noDBRef\");}\n"
"if(typeof(BinData)!
=\"undefined\"){\n"
"BinData.prototype.tojson=function(){\n"
"return\"BinDatatype:
\"+this.type+\"len:
\"+this.len;}}\n"
"else{\n"
"print(\"warning:
noBinData\");}\n"
"if(typeof_threadInject!
=\"undefined\"){\n"
"print(\"fork()available!
\");\n"
"Thread=function(){\n"
"this.init.apply(this,arguments);}\n"
"_threadInject(Thread.prototy