我正试图将大量数据插入MongoDb并从集合中检索随机键值。插入大约2GB的数据需要大约29分钟,检索键值对需要几个小时。与其他NoSQL数据存储相比,这是非常慢的。什么可能是瓶颈
插入
while ((strLine = br.readLine()) != null) {
documentDetail.clear();
//String strLines = strLine.replaceAll("[-+.^:,]","");
tokens = strLine.split("t");
documentDetail.put("key", tokens[0]);
documentDetail.put("value", tokens[1]);
start = System.currentTimeMillis();
collection.insert(documentDetail);
elapsed = System.currentTimeMillis()-start;
totalElapsed += elapsed;
}
BasicDBObject index = new BasicDBObject();
index.put("key", 1);
index.put("value", 1);
collection.ensureIndex(index);
选择 BasicDBObject find = new BasicDBObject();
find.put("value", 1);
while ((strLin = br.readLine()) != null) {
BasicDBObject query = new BasicDBObject();
query.put("key", strLin);
System.out.println(strLin);
start = System.currentTimeMillis();
DBCursor cursorDoc = collection.find(query, find);
while (cursorDoc.hasNext()) {
System.out.println(cursorDoc.next());
}
elapsed = System.currentTimeMillis()-start;
totalElapsed += elapsed;
System.out.println("Reading Ends...");
//field.clear();
}
-
你的索引适合RAM吗?如果没有,MongoDB会很不高兴(=慢)。
> db.collection.totalIndexSize()
-
要使其成为仅扫描/覆盖索引,请使用以下命令:
BasicDBObject query = new BasicDBObject("key",true).append("_id",false);