Last active
August 29, 2015 14:08
-
-
Save achille/f163ec4deecc729f16fa to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// compares MongoDB regular Indexes vs using MultiKey & Elematch | |
/* // regular index: | |
{ "field0": 0, | |
"field1": 1, | |
"field2": 2 ... etc } | |
MultiKey index: | |
{ "props": [ | |
{ "field": "field0", "value": 0 }, | |
{ "field": "field1", "value": 1 }, | |
{ "field": "field2", "value": 2 }, | |
.. etc ] } */ | |
function compareIndexes(numFields,numDocuments) { | |
db=db.getSiblingDB("testIndexes"); | |
db.dropDatabase() | |
var counter = 0; | |
//create regular indexes | |
for(i=0;i<numFields;i++){ | |
indexdoc = {} | |
indexdoc["field"+i]=1 | |
db.regular.ensureIndex(indexdoc); | |
} | |
//create multikey index | |
db.fancy.ensureIndex({"props.field":1,"props.value":1}); | |
//create regular document | |
var start = new Date().getTime(); | |
for(j=0;j<numDocuments;j++){ | |
var doc = { } | |
for(i=0;i<numFields;i++){ | |
doc["field"+i]=counter++ | |
} | |
db.regular.insert(doc); | |
} | |
var regularInsertTime=new Date().getTime()-start; | |
//query regular document | |
counter = 0 | |
var start = new Date().getTime(); | |
for(j=0;j<numDocuments;j++){ | |
var resultf = db.regular.findOne({field0:counter}); | |
counter = counter + numFields; | |
} | |
var regularFindTime=new Date().getTime()-start; | |
//create fancy array thingamajigs whatchamacallit | |
counter = 0; | |
var start = new Date().getTime(); | |
for(j=0;j<numDocuments;j++){ | |
var doc = [ ] | |
for(i=0;i<numFields;i++){ | |
doc.push({field: "field"+i, value: counter++}) | |
} | |
db.fancy.insert({"props":doc}); | |
} | |
var fancyInsertTime=new Date().getTime()-start; | |
//query fancy document | |
counter = 0 | |
var start = new Date().getTime(); | |
for(j=0;j<numDocuments;j++){ | |
var resultf = db.fancy.findOne({props:{$elemMatch:{"field":"field0","value":counter}}}) | |
counter = counter + numFields; | |
} | |
var fancyFindTime=new Date().getTime()-start; | |
//compile results | |
result={numFields:numFields,numDocuments:numDocuments, | |
regular: {insertTime: regularInsertTime, | |
indexSize: db.regular.stats().totalIndexSize, | |
findTime: regularFindTime}, | |
fancy: { insertTime: fancyInsertTime, | |
indexSize: db.fancy.stats().totalIndexSize, | |
findTime: fancyFindTime } | |
} | |
return result; | |
} | |
//run tests various index and doc size combinations | |
for(docs=1000;docs<1000001;docs=docs*10){ | |
for(indexes=1;indexes<64;indexes=indexes+3){ | |
print("Testing with: \tIndexes: " + indexes + "\tDocs: " + docs ) | |
result = compareIndexes(indexes,docs) | |
db.getSiblingDB("indexTest").results.insert(result); | |
} | |
} | |
//print results, insert | |
for(indexes=1;indexes<64;indexes=indexes+3){ | |
var string = "Indexes, " + indexes + ","; | |
for(docs=1000;docs<1000001;docs=docs*10){ | |
res = db.getSiblingDB("indexTest").results.findOne({numFields:indexes,numDocuments:docs}); | |
if(res != null){ | |
var regular = res.regular.findTime; | |
var fancy = res.fancy.findTime; | |
var slowdown = (fancy+regular)/(regular) | |
string = string + slowdown + "," | |
} else { | |
string = string + 0 + "," | |
} | |
} | |
print(string) | |
} | |
//print results, query | |
for(indexes=1;indexes<64;indexes=indexes+3){ | |
var string = "Indexes, " + indexes + ","; | |
for(docs=1000;docs<1000001;docs=docs*10){ | |
res = db.getSiblingDB("indexTest").results.findOne({numFields:indexes,numDocuments:docs}); | |
if(res != null){ | |
var regular = res.regular.insertTime; | |
var fancy = res.fancy.insertTime; | |
var slowdown = (fancy+regular)/(regular) | |
string = string + slowdown + "," | |
} else { | |
string = string + 0 + "," | |
} | |
} | |
print(string) | |
} | |
//print results, query | |
for(indexes=1;indexes<64;indexes=indexes+3){ | |
var string = "Indexes, " + indexes + ","; | |
for(docs=1000;docs<1000001;docs=docs*10){ | |
res = db.getSiblingDB("indexTest").results.findOne({numFields:indexes,numDocuments:docs}); | |
if(res != null){ | |
var regular = res.regular.indexSize; | |
var fancy = res.fancy.indexSize; | |
var slowdown = (fancy+regular)/(regular) | |
string = string + slowdown + "," | |
} else { | |
string = string + 0 + "," | |
} | |
} | |
print(string) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment