gpt4 book ai didi

java - MongoDB 索引在 find 方法上性能更好?

转载 作者:行者123 更新时间:2023-12-01 14:17:25 26 4
gpt4 key购买 nike

我有一个关于 MongoDB 索引的问题。 (我使用 mongo-java-driver)

如果数据库包含许多对象,所有对象都具有完全相同的结构,唯一的区别是某些 ID 字段的值和名称。对集合中的ID字段建立索引会加快ID字段上某个对象后的查询速度吗?

我使用 MongoHQ“云”MongoDB,也许我做错了什么,但在这种情况下索引不会获得更好的性能。

感谢您的宝贵时间。

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

最佳答案

我在研究java mongo驱动程序时尝试了类似的方法,并得到了相同的结果。没有索引的搜索比使用索引更好(响应时间)......

我的提示是:在 shell 上连接并使用命令“explain”....这有助于分析正在发生的情况。

关于java - MongoDB 索引在 find 方法上性能更好?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17994049/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com