Search code examples
javamongodbmongodb-javamongo-javadatabase

MongoDB select's/find seems to have the same performance for a larger amount of data


I'm measuring the performance of MongoDB with its native driver. For the insert operation, the more I inserted the more time it took. However, with selecting, I have 500.000 documents (inside 1 collection) with each document having 4 other embedded, small documents inside of it.

I performed a select query using also a conditional operation for 1 subdocument. The performance of selecting 100 documents (from a total of 1000) is 0,007 ms while selecting 50000 documents (from a total of 500000) is 0,008 ms.

I don't quite understand how this is possible. Is it really that fast? I'm measuring the performance using JMH.


Solution

  • Your test setup only measures the time it takes to open a cursor, but not to actually read any data from the cursor.

    When you perform contactCollection.find(bs); the database returns a cursor object but doesn't actually do anything yet. The actual query doesn't happen before you retrieve the data from the cursor. Changing the line to contactCollection.find(bs).toArray() should force the database to return all results, allowing you to measure both the query-time, the time it takes to pass the results via network and for the Java driver to parse the results.

    Alternatively you could use contactCollection.find(bs).explain() to measure the time the operation takes only on the database-level and also get a DBObject with some useful performance metrics from the database itself.