-
Notifications
You must be signed in to change notification settings - Fork 4
/
bulkVsRegularRemove.js
74 lines (54 loc) · 1.7 KB
/
bulkVsRegularRemove.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
db = db.getSiblingDB('test');
var collections = [ "foo", "foo2", "bar", "bar2"];
collections.forEach( function(coll) {
db.getCollection(coll).drop();
db.createCollection(coll);
db.getCollection(coll).createIndex( { "x" : 1 });
});
var loadData = function() {
for (var i=0;i<10000;i++) {
var x = Math.floor(Math.random()*3);
collections.forEach( function(coll) {
db.getCollection(coll).insert({_id:i, "x" : x});
});
}
}
loadData();
var query = { "x" : Math.floor(Math.random()*3) };
printjson(query);
var totalCount = db.foo.find(query).count();
print("totalCount="+totalCount);
var bulkDelete = function() {
print("bulkDelete");
var count = 0;
var fooCursor = db.foo.find(query);
var fooBulk = db.foo.initializeUnorderedBulkOp();
var barBulk = db.bar.initializeUnorderedBulkOp();
while ( fooCursor.hasNext() ) {
var doc = fooCursor.next();
fooBulk.find( { "_id" : doc._id } ).remove();
barBulk.find( { "_id" : doc._id } ).remove();
}
var result = fooBulk.execute();
printjson(result);
result = barBulk.execute();
printjson(result);
}
var regularDelete = function() {
print("regularDelete");
var count = 0;
db.foo2.find(query).forEach( function(doc) {
db.foo2.remove( { "_id" : doc._id } );
db.bar2.remove( { "_id" : doc._id } );
});
}
var timeFunction = function(f) {
var start = (new Date()).getTime();
f();
var runtime =(new Date().getTime()-start);
print( "Runtime: " + runtime );
return runtime;
}
var btime = timeFunction(bulkDelete);
var rtime = timeFunction(regularDelete);
print( "Bulk is " + (rtime/btime)*100 + "% faster" );