简体   繁体   English

性能Mongodb java驱动

[英]Performance Mongodb java driver

I'm using mongodb java driver in my project for execute queries (finds, aggregate, mapreduce,...) over a big collection (5 millions of documents) 我在我的项目中使用mongodb java驱动程序在一个大集合中执行查询(查找,聚合,mapreduce,...)(500万个文档)

The driver version is: 驱动程序版本是:

<!-- MongoDB driver-->
<dependency>
  <groupId>org.mongodb</groupId>
  <artifactId>mongo-java-driver</artifactId>
  <version>3.0.3</version>
</dependency>

My problem is when I use the api find with some filters from java, the operation takes 15 sec. 我的问题是当我使用api find和java中的一些过滤器时,操作需要15秒。

....
Iterable<Document> messageList = collection.find().filter(... some filters).sort(... fields);

// Find documents
for (Document message : messageList) {
....
// some code
....
}

I check the mongo server log file and see the trace is a COMMAND instead of a QUERY : 我检查mongo服务器日志文件,看到跟踪是一个COMMAND而不是QUERY

2015-09-01T12:11:47.496+0200 I COMMAND [conn503] command b.$cmd command: count { count: "logs", query: { timestamp: { $gte: new Date(1433109600000) }, aplicacion: "APP1", event: "Event1" } } planSummary: IXSCAN { timestamp: 1, aplicacion: 1 } keyUpdates:0 writeConflicts:0 numYields:19089 reslen:44 locks:{ Global: { acquireCount: { r: 19090 } }, MMAPV1Journal: { acquireCount: { r: 19090 } }, Database: { acquireCount: { r: 19090 } }, Collection: { acquireCount: { R: 19090 } } } 14297ms 2015-09-01T12:11:47.496 + 0200 I COMMAND [conn503]命令b。$ cmd命令:count {count:“logs”,查询:{timestamp:{$ gte:new Date(1433109600000)},aplicacion:“ APP1“,事件:”Event1“}} planSummary:IXSCAN {timestamp:1,aplicacion:1} keyUpdates:0 writeConflicts:0 numYields:19089 reslen:44 locks:{Global:{acquireCount:{r:19090}},MMAPV1Journal :{acquireCount:{r:19090}},数据库:{acquireCount:{r:19090}},收藏:{acquireCount:{R:19090}}} 14297ms

If I run the same query from mongodb client (Robomongo), it takes 0.05 ms. 如果我从mongodb客户端(Robomongo)运行相同的查询,则需要0.05毫秒。

db.getCollection('logs').find({ timestamp: { $gte: new Date(1427839200000) }, aplicacion: "APP1", event: "Event1" })

and in the server log is as QUERY 并在服务器日志中为QUERY

All queries that are made (find, aggregate, ...) with the driver java commands are transformed? 使用驱动程序java命令进行的所有查询(查找,聚合,...)都会被转换? The performance is much worse than mongo shell. 性能比mongo shell差很多。

I think the issue is when you ran the query in mongo shell it will return only top 20 result at a time, here you are trying to read all the document and put it into array 我认为问题是当你在mongo shell中运行查询时,它一次只返回前20个结果,在这里你试图读取所有文档并将其放入数组中

try this query and see 试试这个查询,看看

List messageList = collection.find(filter).sort(...field).limit(20).into(new ArrayList()); 列表messageList = collection.find(filter).sort(... field).limit(20).into(new ArrayList());

It's highly recommended to create a index on query field. 强烈建议在查询字段上创建索引。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM