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);
}
noBillSide
  • 528
  • 5
  • 18
  • Could you show some queries that you do? – Miguel Cartagena Aug 01 '13 at 12:36
  • yes it is just for testing so, because I will compare the performance with different databases. First contact on MongoDB. – noBillSide Aug 01 '13 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 – Sammaye Aug 01 '13 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 :) – noBillSide Aug 01 '13 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? – Philipp Aug 01 '13 at 12:55
  • Its the same right, just the counter differs. (Thats because I am going to do tests with parallel reads and writes from different machines later) – noBillSide Aug 01 '13 at 12:57

2 Answers2

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.

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.

noBillSide
  • 528
  • 5
  • 18