Question

I have a question regarding indexing in MongoDB. (I use the mongo-java-driver)

If the database contains many objects all of them have exact the same structure and the only differences are lets say, the value of some ID field and a name. Will indexing the ID field in the collection speed up the query after some certain object on the ID field?

I use MongoHQ "Cloud" MongoDB, maybe I am doing something wrong but indexing in this case wont get any better performance.

Thanks for your time.

/* just for testing */
DBCollection table = db.getCollection("user");
table.createIndex(new BasicDBObject("uuid", 1));
....

/* write */
for (int i = 0; i < numberOfInserts; i++) {
    BasicDBObject document = new BasicDBObject();
    document.put("name", "hello");
    document.put("uuid", randomUUID.toString() + i);
    table.insert(document);
}

....
/* read */
for (int i = 0; i < numberOfInserts; i++) {
        whereQuery.put("uuid", randomUUID.toString() + i);
        DBObject findOne = table.findOne(whereQuery);
}
Was it helpful?

Solution 2

Well it's a little bit embarrassing, but the reason why the indexing didn't lead to performance gain was simply because the huge distance between local client and remote database. The indexing just did not made an impact on query time. With tests on clients relativly close to the database indexing brought clearly some performance.

OTHER TIPS

I tried something like this while studied java mongo driver, and got the same result. An search without index was better (response time) than using an index....

My tip is: Connect on shell and use command "explain"....it's helpfull to analyse what is going on.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top