noBillSide
noBillSide

Reputation: 528

MongoDB Index better performance on find method?

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);
}

Upvotes: 0

Views: 332

Answers (2)

noBillSide
noBillSide

Reputation: 528

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.

Upvotes: 0

giullianomorroni
giullianomorroni

Reputation: 29

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.

Upvotes: 1

Related Questions