为什么只有当收藏不存在时,MongoDB才会插入记录的速度较慢?
我为了好玩而对MongoDB 3.2.17进行了基准测试,并且无法理解这种行为的原因。
当我在插入前创建一个空集合
MongoDB x 906 ops / sec±2.78%(75次采样)
  当我不创建任何空的集合,只是简单地运行insertMany 
MongoDB x 87.81 ops / sec±94.31%(71次采样)// 错误率很高,为什么?
我的代码使用Benchmark.js,以便您可以指出我是否犯了一些错误
var Benchmark = require('benchmark');
var suite = new Benchmark.Suite;
const MongoClient = require('mongodb').MongoClient;
var collectionMongodb = 'testrecords2';
Promise.all([
    MongoClient.connect('mongodb://localhost:27017/testme') 
]).then((clients) => {
    var nativeCollection = clients[0].db('testmongodb').collection(collectionMongodb)
    var records = [];
    for (var i = 0; i < 100; i++) {
        records.push({
            name: 'bugwheels' + i,
            interest: 'Not Many',
            job: 'Useless'
        })
    }
    suite
    .add('MongoDB', {
        defer: true,
        fn: function(def) {
            nativeCollection.insertMany(records, (e, r) => {
                def.resolve();
                // console.log(r)
            })
        }
    })
    .on('cycle', function(event) {
        console.log(String(event.target));
    })
    .on('complete', function() {
        console.log('Fastest is ' + this.filter('fastest').map('name'));
//          nativeCollection.drop()
    })
    .run({ 'async': true });    
})
请让我知道出了什么问题?
我的StorageEngine
{
    "name" : "wiredTiger",
    "supportsCommittedReads" : true,
    "persistent" : true
 }
我开始使用mongoDB:
mongod --dbpath ./db
这很简单。 您在每次运行中插入相同的100条记录。
当您在每次运行之间删除集合时,您需要测量删除集合需要多长时间,然后将100个文档插入到集合中。
当你注销掉集合时,你在第一次运行时插入了100条记录,但是后来的每次运行都试图将完全相同的100个文档插入到同一个集合中,并且它们都出现错误:
exception: E11000 duplicate key error collection: testmongodb.testrecords2 index: _id_ dup key: { : ObjectId('5aa19388df671d3a065076f5') } code:DuplicateKey
我假设你创建空集合的方式实际上会导致工作量差异很大,所以你应该做的一件事就是确保每次生成独特的记录都能正确地进行基准测试。
链接地址: http://www.djcxy.com/p/41357.html上一篇: Why is MongoDB slower to insert Records only when collection does not exist?
下一篇: How can I load a 3d model in Aframejs? It currently works fine in threejs
