0

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);
}
6
  • Could you show some queries that you do? Commented Aug 1, 2013 at 12:36
  • yes it is just for testing so, because I will compare the performance with different databases. First contact on MongoDB. Commented Aug 1, 2013 at 12:44
  • Have you tested this with and without indexes? Can you show us the explain()?, also what classes as good performance? We have no benchmark to go by Commented Aug 1, 2013 at 12:47
  • I have tested it at least without adding indexes by myself! the findOne method has no explain method if I am right... I am just wondering why it isn't getting faster. I will not classify anything :) Commented Aug 1, 2013 at 12:53
  • Your code doesn't include anything for generating the randomUUID, so I assume that it's the same for every document. Is that correct? Commented Aug 1, 2013 at 12:55

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

Comments

0

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.