gpt4 book ai didi

javascript - 为什么只有在集合不存在时 MongoDB 才插入记录的速度较慢?

转载 作者:IT老高 更新时间:2023-10-28 13:22:51 26 4
gpt4 key购买 nike

我在做 MongoDB 3.2.17 的基准测试 为了好玩,无法理解这种行为的原因。

  • 当我在插入之前创建一个空集合时

    MongoDB x 906 ops/sec ±2.78% (75 runs sampled)

  • 当我不创建任何空集合而只是简单地运行 insertMany

    MongoDB x 87.81 ops/sec ±94.31% (71 runs sampled) // Error Rate is high, why?

我的代码使用 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 });
})

请告诉我出了什么问题?

我的存储引擎

{
"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

我假设您创建空集合的方式实际上会导致工作量发生显着变化,因此您应该做的一件事是通过每次生成唯一记录来确保您正确地进行基准测试。

关于javascript - 为什么只有在集合不存在时 MongoDB 才插入记录的速度较慢?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49110277/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com