Skip to content

Instantly share code, notes, and snippets.

@jmikola
Created July 26, 2012 20:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jmikola/3184175 to your computer and use it in GitHub Desktop.
Save jmikola/3184175 to your computer and use it in GitHub Desktop.
Compare Mongo read behavior
/**
* Initialize the database collection for querying.
*/
var init = function() {
db.foo.drop();
for (var i = 0; i < 10; ++i) {
for (var j = i; j < 100000; j += 10) {
db.foo.insert({x: j, y: j});
}
}
db.foo.ensureIndex({x:1});
db.foo.ensureIndex({y:1});
}
/**
* Execute a given number of queries stepping through a range of values.
*
* @param integer num
* @param integer from
* @param integer to
* @return Total nscanned from all query explains
*/
var query = function(num, from, to) {
var step = Math.floor((to - from) / num);
var nscanned = 0;
for (var i = from; i < to; i += step) {
nscanned += db.foo.find({x: {$lt: i}, y: {$gt: (i - 20)}}).explain().nscanned;
}
return nscanned;
}
if (db.foo.count() !== 100000) {
init();
}
var tests = [
{
name: "One batch of 100 queries in the 75-100k range",
batches: [
[100, 75000, 99999]
]
},
{
name: "Four batches of 25 queries in the 0-25, 25-50, 50-75, and 75-100k ranges",
batches: [
[25, 0, 24999],
[25, 25000, 49999],
[25, 50000, 74999],
[25, 75000, 99999]
]
}
];
tests.forEach(function(test) {
jsTest.log(test.name);
var start = new Date();
var btreeStart = db.serverStatus().indexCounters.btree;
var info;
test.batches.forEach(function(args, i) {
print("batch " + (i + 1) + " nscanned: " + query.apply(null, args));
});
var end = new Date();
var btreeEnd = db.serverStatus().indexCounters.btree;
print("total time: " + ((end.getTime() - start.getTime()) / 1000));
["accesses", "hits", "misses", "resets", "missRatio"].forEach(function(stat) {
print("btree." + stat + ": " + (btreeEnd[stat] - btreeStart[stat]));
});
});
[honeydew: FREE-14832_read_behavior] $ mongo reads.js
MongoDB shell version: 2.0.6
connecting to: test
----
One batch of 100 queries in the 75-100k range
----
batch 1 nscanned: 1269469
total time: 3.379
btree.accesses: 0
btree.hits: 0
btree.misses: 0
btree.resets: 0
btree.missRatio: 0
----
Four batches of 25 queries in the 0-25, 25-50, 50-75, and 75-100k ranges
----
batch 1 nscanned: 324675
batch 2 nscanned: 974675
batch 3 nscanned: 975800
batch 4 nscanned: 325819
total time: 6.688
btree.accesses: 0
btree.hits: 0
btree.misses: 0
btree.resets: 0
btree.missRatio: 0
[honeydew: FREE-14832_read_behavior] $ mongo reads.js
MongoDB shell version: 2.2.0-rc1-pre-
connecting to: test
----
One batch of 100 queries in the 75-100k range
----
batch 1 nscanned: 1269469
total time: 5.956
btree.accesses: 0
btree.hits: 0
btree.misses: 0
btree.resets: 0
btree.missRatio: 0
----
Four batches of 25 queries in the 0-25, 25-50, 50-75, and 75-100k ranges
----
batch 1 nscanned: 324675
batch 2 nscanned: 974675
batch 3 nscanned: 975800
batch 4 nscanned: 325819
total time: 11.918
btree.accesses: 0
btree.hits: 0
btree.misses: 0
btree.resets: 0
btree.missRatio: 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment