Skip to content

Instantly share code, notes, and snippets.

@freeeve
Created January 28, 2012 22:38
Show Gist options
  • Save freeeve/1696041 to your computer and use it in GitHub Desktop.
Save freeeve/1696041 to your computer and use it in GitHub Desktop.
mongodb count benchmark index
var filters = ['abcd', 'efgh', 'ijkl', 'mnop', 'qrst', 'uvwx', 'yz'];
var flen = filters.length;
var n = 1000000;
var max = -1;
function printInterestingStats(explain) {
print(" cursor: " + explain.cursor);
print(" nscanned: " + explain.nscanned);
print(" scanAndOrder: " + explain.scanAndOrder);
print(" millis: " + explain.millis);
}
function getRandomString() {
return ''+Math.floor(Math.random()*max);
}
function getRandomFilter() {
return filters[Math.floor(Math.random()*flen)];
}
function initialInsert() {
for( var j = 0; j < flen; j++) {
for( var i = 0; i < max; i++) {
var dStr = ''+i;
var fStr = filters[j];
db.test9.insert(
{
filter: fStr,
data: dStr,
count:1
}
);
}
}
}
function updateCounts() {
for( var i = 0; i < n; i++) {
var dStr = getRandomString();
var fStr = filters[Math.floor(Math.random()*flen)];
db.test9.update(
{
filter: fStr,
data: dStr
},
{
$inc: {count: 1}
}
);
}
}
function bench() {
max = 100000;
print("benchmarking with index on {filter,data}, 700K docs");
db.test9.drop();
db.test9.ensureIndex({filter: 1, data: 1});
d = new Date();
initialInsert();
tot = new Date() - d;
per = tot/(flen*max);
print("initialInsert of "+(flen*max)+" done in: " + tot + "ms, "+per+"ms per insert");
d = new Date();
updateCounts();
tot = new Date() - d;
per = tot/n;
print("updateCounts "+ n +" times done in: " + tot + "ms, "+per+"ms per update");
print("explain find({filter:\"abcd\"}).sort({count:-1}): ");
printInterestingStats(db.test9.find({filter:"abcd"}).sort({count:-1}).explain());
print("explain find({filter:\"abcd\"}).limit(100).sort({count:-1}): ");
printInterestingStats(db.test9.find({filter:"abcd"}).limit(100).sort({count:-1}).explain());
print("benchmarking with index on {filter,data} and {filter, count}, 700k docs");
db.test9.drop();
db.test9.ensureIndex({filter: 1, data: 1});
db.test9.ensureIndex({filter: 1, count: -1});
d = new Date();
initialInsert();
tot = new Date() - d;
per = tot/(flen*max);
print("initialInsert of "+(flen*max)+" done in: " + tot + "ms, "+per+"ms per insert");
d = new Date();
updateCounts();
tot = new Date() - d;
per = tot/n;
print("updateCounts "+ n +" times done in: " + tot + "ms, "+per+"ms per update");
print("explain find({filter:\"abcd\"}).sort({count:-1}): ");
printInterestingStats(db.test9.find({filter:"abcd"}).sort({count:-1}).explain());
print("explain find({filter:\"abcd\"}).limit(100).sort({count:-1}): ");
printInterestingStats(db.test9.find({filter:"abcd"}).limit(100).sort({count:-1}).explain());
max = 1000000;
print("benchmarking with index on {filter,data}, 7M docs");
db.test9.drop();
db.test9.ensureIndex({filter: 1, data: 1});
d = new Date();
initialInsert();
tot = new Date() - d;
per = tot/(flen*max);
print("initialInsert of "+(flen*max)+" done in: " + tot + "ms, "+per+"ms per insert");
d = new Date();
updateCounts();
tot = new Date() - d;
per = tot/n;
print("updateCounts "+ n +" times done in: " + tot + "ms, "+per+"ms per update");
print("explain find({filter:\"abcd\"}).sort({count:-1}): ");
print("too big to sort without limit!");
//printInterestingStats(db.test9.find({filter:"abcd"}).sort({count:-1}).explain());
print("explain find({filter:\"abcd\"}).limit(100).sort({count:-1}): ");
printInterestingStats(db.test9.find({filter:"abcd"}).limit(100).sort({count:-1}).explain());
print("benchmarking with index on {filter,data} and {filter, count}, 7M docs");
db.test9.drop();
db.test9.ensureIndex({filter: 1, data: 1});
db.test9.ensureIndex({filter: 1, count: -1});
d = new Date();
initialInsert();
tot = new Date() - d;
per = tot/(flen*max);
print("initialInsert of "+(flen*max)+" done in: " + tot + "ms, "+per+"ms per insert");
d = new Date();
updateCounts();
tot = new Date() - d;
per = tot/n;
print("updateCounts "+ n +" times done in: " + tot + "ms, "+per+"ms per update");
print("explain find({filter:\"abcd\"}).sort({count:-1}): ");
printInterestingStats(db.test9.find({filter:"abcd"}).sort({count:-1}).explain());
print("explain find({filter:\"abcd\"}).limit(100).sort({count:-1}): ");
printInterestingStats(db.test9.find({filter:"abcd"}).limit(100).sort({count:-1}).explain());
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment