首页 > 代码库 > Mongodb 之insert瞬时完成,带测试数据---飞天博客

Mongodb 之insert瞬时完成,带测试数据---飞天博客

这几天在看mongdb官网,然后经运行插入数据,突然发现这个数据确实很强大,这里用数据说话,我用的普通pc机,amd的双核 2.7GHz,4G内存,当然在测试的时候系统不是只作为数据库服务器的,还运行了myeclipse,qq,杀毒软件什么的,当然在测试的时候cpu大约是95%的负载

具体什么结果呢?

刚开始测的时候,没注意,一下启动了10个线程,每个线程插10000条数据,结果,系统不堪负载,断电黑屏了,这电脑有点问题。这下囧了;

好吧,然后在慢慢的摸索之下,插入10000条数据,改为插入1000条数据,10个线程改为100个线程,测试结果如下:mongodb 100个线程,每个线程insert 1000次,76760 ms 一分10s的样子。关系型数据库还是很厉害的

然后我又具体测试了一下500个线程,每个线程同时插入10条记录,请看下面记录:下面的格式为:线程id:线程完成时间;总共结束时间:5542

262:645173:4896388:505757:5351
460:561222:4874102:5230149:5387
18:2183298:4752109:5233384:5179
456:2105301:4768386:5059396:5181
46:3060176:4895289:512632:5352
412:2851189:4918268:5132437:4935
448:2909133:4933209:5259444:4935
462:3363231:4919140:5259101:5357
425:342391:4900348:5116459:4939
461:341772:4898249:5260418:4938
458:3507365:474873:5239451:4940
450:3544221:4941389:5074147:5399
452:358599:4927148:5265142:5398
428:3662299:4816152:5275404:5193
454:3735241:4951286:5119166:5396
423:3820243:496728:524251:5366
259:4270119:496945:5246202:5367
349:413919:5002107:5249489:4632
421:3872363:4792247:5269484:4632
188:4285223:4973103:5249442:4948
96:4284383:4801245:5271435:4949
414:404925:5023391:5086491:4637
426:3881227:499886:5250495:4637
424:3945371:482283:5252497:4638
416:4122225:5008160:5283143:5413
33:4384367:4831354:5101499:4641
379:422852:4990116:5253145:5417
420:3983179:501950:5253493:4644
310:4275422:4605382:5082480:4646
131:4428256:500390:5256482:4647
201:4427121:507029:5257485:4647
67:4410123:5074380:5085487:4648
203:4496261:5054242:525993:5385
252:455039:505743:5260138:5421
308:4477233:509278:5260483:4653
343:449335:5076127:5287481:4654
403:4448417:472395:5265157:5425
3:4685430:4661385:5103465:4659
306:4495237:5109183:5290406:5217
401:4456125:5111251:5288469:4662
230:4626253:5111376:5093471:4664
419:4225229:512064:5268476:4657
110:4627239:5120378:5097477:4660
94:4640235:5126248:5256467:4664
38:463936:5105270:5168479:4661
405:4483273:5006269:5171478:4660
126:4659272:4987295:5165473:4670
294:4541360:4958181:5298463:4672
364:4508192:5122337:5162472:4661
134:4681362:4963361:5120408:5228
320:4557113:5125198:5278470:4664
284:456062:5126236:5280468:4664
359:4538150:5155207:530379:5404
357:4540117:5135373:5120141:5441
358:45417:520041:5281474:4674
296:457588:5141372:5107429:4986
53:47015:520810:5351466:4670
13:4772114:5143400:511056:5407
54:4708328:501958:5282431:4987
266:461455:5146312:5160427:4989
297:4610283:504014:5359486:4673
255:4741282:5021395:5127475:4684
197:474370:5146369:5132488:4672
300:4596316:502465:5292159:5449
375:4563351:502884:5293492:4675
195:4749274:5025156:5326494:4674
200:4731254:5138263:5294410:5242
69:473149:5155196:5297490:4677
292:4607279:5049144:5330139:5453
120:4739336:5033210:5299409:5252
311:4628212:5160158:5329433:4997
40:473911:5224281:5194464:4681
98:4742250:5146129:5324498:4679
37:474497:5165169:5337208:5422
66:4742399:5003162:5332496:4681
213:476974:5171370:512812:5492
106:4748285:5065353:5182500:4677
215:4774187:5197394:513176:5423
171:4777340:5050167:5341180:5426
350:4632124:5175455:4885333:5313
63:4755112:5178185:5331318:5302
104:475547:5180368:5136322:5302
288:4630194:5182151:5345218:5428
9:4822352:5060163:5348228:5428
303:464675:5183118:5313130:5428
68:4757100:5183153:5349238:5431
87:476534:5183338:5190204:5432
214:4766332:5062324:5190111:5434
366:4591271:5083154:534727:5434
122:476671:5193377:51566:5504
217:4795258:5179390:514560:5434
244:4784246:5180265:5216182:5438
240:4786184:51974:53898:5508
44:4788161:5218164:534926:5437
287:4703307:5084267:5217331:5327
339:4701309:50831:539442:5440
407:465015:5263155:5357313:5330
193:4839132:5198330:520024:5439
260:4810277:5093342:520092:5443
186:4821257:5220436:4903329:5331
128:4819135:5243453:4906345:5323
341:470630:5199447:4908290:5320
178:4823397:5037172:5360234:5447
335:4709314:5080432:4909327:5334
146:4842326:5080445:4912278:5322
305:471780:5205443:4912276:5323
226:483185:5207168:5364325:5339
302:4707211:5231402:5161356:5296
220:483777:5210174:5360319:5343
224:483731:5210434:4913381:5292
199:4861293:5100446:4916264:5450
347:471822:5210441:491889:5457
219:4865280:5087438:4918108:5457
190:484420:5211457:4920232:5458
355:4692315:5108439:492182:5458
115:4848137:5260440:492016:5514
411:4628393:5058398:5172136:5500
17:490348:5221449:4923346:5338
413:4637334:51002:5416291:5352
177:4884392:505221:5400323:5351
415:4639206:5226374:5174321:5351
61:4864105:5227387:5184344:5340
304:4739216:522823:5402317:5352
175:488959:5228165:5385191:5489
275:4764205:5252170:538081:5467

我这台机器上最大连接数为:500,所以没测试更多,但是看到这上面的效率是很好的啦,前面的线程几乎是秒插入进去。如果要做个500个人同时注册,单机mongodb就是小case。当然加大它的连接数。注意,这是连接数,不是MongoClient的个数,默认实例化mongClient的个数是为1的

从后台可以看到:db.mydb.count() 结果为:5000,说明这次测试结果全部没有出差错,速度之快,当然不用说,也查了一下原因,但和我做的实验实际还是有点出入:

资料如下:

但mongodb的这种操作,客户端将文档发送给服务器之后就like干别的了。客户端也收不到“好的,知道了”或者“有问题,能重新传送一遍嘛?”这类响应。这个特点的有点很明显,速度块,这些操作都会非常块的;但服务器如果出问题,客户端不知道,比如服务器奔溃,断电什么的,客户端还是会继续发送写操作。

但是我下的这个mongodb,还是有出入的,因为当我关闭服务器,客户端报错了!!!证明服务器出问题还是会被检测到。至于发送写操作能否验证是否成功,这个问题,java驱动会返回一个WriterResult结果,里面可以返回最近一次出错信息,想必这个版本默认就设置了“安全操作”,也就是说,插入操作会返回信息,待用户判断是否将上一次为插入成功的数据再一次插入进数据库。“安全操作”在我看来还是很快的,如果将“安全操作”设置为“非安全操作”版,插入速度应该会更快

这里附上mongodb的java驱动之crud

public class MongoTest {
	

	public static void main(String[] args) throws UnknownHostException {
		MongoClient mongoClient = new MongoClient( "localhost" , 30000 );
		DB db = mongoClient.getDB( "mytest" );
	// 返回当前的数据库名称
//		Set<String> colls = db.getCollectionNames();
//		
//		for (String s : colls) {
//		    System.out.println(s);
//		}
		DBCollection collection=db.getCollection("test");
		BasicDBObject doc = new BasicDBObject("name", "xiaohua2");
		collection.insert(doc);
			System.out.println(collection.count());
		mongoClient.close();
		/*
	
		//得到一个集合,这个集合就是用来做crud的接口
		DBCollection coll = db.getCollection("mydb");
		//插入一个document,和sql的表差不多
		//The _id element has been added automatically by MongoDB to your document.
		//Remember, MongoDB reserves element names that start with “_”/”$” for internal use
		BasicDBObject doc = new BasicDBObject("name", "MongoDB")
        .append("type", "database")
        .append("count", 1)
        .append("info", new BasicDBObject("x", 203).append("y", 102));
		coll.insert(doc);
		//得到第一条document
		DBObject myDoc = coll.findOne();
		System.out.println(myDoc);
		//多条数据插入
		for (int i=0; i < 100; i++) {
		    coll.insert(new BasicDBObject("i", i));
		}
		//统计document的行数
		System.out.println(coll.getCount());
		
		//使用游标
		DBCursor cursor = coll.find();
		try {
		   while(cursor.hasNext()) {
		       System.out.println(cursor.next());
		   }
		} finally {
		   cursor.close();
		}
		mongoClient.close();
		//查询
//		BasicDBObject query = new BasicDBObject("i", 71);
//		cursor = coll.find(query);
//
//		try {
//		   while(cursor.hasNext()) {
//		       System.out.println(cursor.next());
//		   }
//		} finally {
//		   cursor.close();
//		}
		  */
	}
}

注意记得将mongoclient close掉


备注,如果转载:请标明出处blog.csdn.net/xh199110 飞天博客

如果有写的不对的地方,欢迎指正。作者也是看官网,查资料,加上自己的理解,写了这篇文章,以便大家一起来学习,谢谢

Mongodb 之insert瞬时完成,带测试数据---飞天博客