简体   繁体   中英

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)

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.

....
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 :

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

If I run the same query from mongodb client (Robomongo), it takes 0.05 ms.

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

and in the server log is as QUERY

All queries that are made (find, aggregate, ...) with the driver java commands are transformed? The performance is much worse than 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

try this query and see

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

It's highly recommended to create a index on query field.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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