-
-
Notifications
You must be signed in to change notification settings - Fork 3.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Performance degradation when upgrading from Mongoose v4 to v8 (findOne slower than find) #14906
Comments
Mongoose version 4.13.21 const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
name: String,
val: Number
});
const Test = mongoose.model('Test', testSchema);
async function main() {
for (let i = 0; i < 100; i++) {
const doc = new Test();
doc.name = 'Test',
doc.val = i;
doc.save(function(err) {
});
}
}
async function run() {
Test.findOne({}, function (err, doc) {
console.log('doc', doc);
});
}
(async () => {
await mongoose.connect('mongodb://localhost:27017/performance-test');
// await mongoose.connection.dropDatabase();
await main();
console.time('performanceTest')
await run();
console.timeEnd('performanceTest');
console.log(mongoose.version)
process.exit(0);
})(); Mongoose version 8.7.0 const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
name: String,
val: Number
});
const Test = mongoose.model('Test', testSchema);
async function main() {
for (let i = 0; i < 100; i++) {
await Test.create({
name: 'Test',
val: i
})
}
}
async function run() {
const res = await Test.findOne();
console.log(res);
}
(async () => {
await mongoose.connect('mongodb://localhost:27017/performance-test');
await mongoose.connection.dropDatabase();
await main();
console.time('performanceTest')
await run();
console.timeEnd('performanceTest');
console.log(mongoose.version)
process.exit(0);
})(); |
if we lean the two do we get the same results ? To know if it's mongoose related or driver related |
lean causes a +/- change of 1 ms. |
So might be driver related I guess |
As for the comparison I made, it was not about saving tests but rather a comparison of the commands like findOne, find, and lean. I am sharing part of a test results. the slowness was observed not only in the actual application connected to MongoDB, but also in a test code using a mongodb-memory-server. Result
[email protected]
[email protected]
Only the contents of the replica connection were implemented according to the version, and there was a difference when index, shard, and schema were all used in the same state |
I'm writing this comment to check if any other tasks are in progress for performance improvement. 😄 |
Considering the following basic script: const mongoose = require('mongoose');
const testSchema = new mongoose.Schema({
name: String,
val: Number
});
const Test = mongoose.model('Test', testSchema);
async function main() {
for (let i = 0; i < 1; i++) {
const doc = new Test();
doc.name = 'Test',
doc.val = i;
await doc.save();
}
}
(async () => {
await mongoose.connect('mongodb://localhost:27017/performance_test_mongoose');
await main();
console.time('findOneTest');
for (let i = 0; i < 1000; ++i) {
const res = await Test.findOne();
}
console.timeEnd('findOneTest');
process.exit(0);
})(); Here's the runtimes I'm seeing locally:
The most pronounced jump is between Mongoose 5 and Mongoose 6. Specifically Mongoose 6.0.0, because 6.0.0 has similar performance number to 6.x. For the sake of comparison, consider the following MongoDB Node driver script
Runtimes:
So the performance degradation from Mongoose 5 to 6 can be fully explained by upgrading from MongoDB Node driver 3 to 4. I'll raise an issue on MongoDB JIRA. I did notice that our fix for #7398 3f52dfb includes some logic that tracks a stack trace every time a query is executed, which is the only potential low hanging fruit we've found so far to noticeably improve Mongoose's handling of |
I opened MongoDB JIRA ticket https://jira.mongodb.org/browse/NODE-6499 to see if there's some feedback from the MongoDB Node driver team, and I'll keep looking for ways to improve |
perf: make a few micro-optimizations to help speed up findOne()
Prerequisites
Mongoose version
8.6.3
Node.js version
v18.20.4
MongoDB version
4.4.25
Operating system
Linux
Operating system version (i.e. 20.04, 11.3, 10)
No response
Issue
Description:
I am currently in the process of upgrading Mongoose from version 4 to version 8 in our project, and I've encountered a significant performance issue.
Summary:
Details:
Steps to Reproduce:
Expected Behavior:
Actual Behavior:
The text was updated successfully, but these errors were encountered: