Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

mongodb: will limit() increase query speed?

Tags:

mongodb

Is db.inventory.find().limit(10) faster than db.inventory.find()?

I have millions of records in mongodb, I want to get top 10 records in some orders.

like image 805
gzc Avatar asked Sep 14 '25 13:09

gzc


1 Answers

Using limit() you inform the server that you will not retrieve more than k documents. Allowing some optimizations to reduce bandwidth consumption and to speed-up sorts. Finally, using a limit clause the server will be able to better use the 32MB max available when sorting in RAM (i.e.: when sort order cannot be obtained from an index).


Now, the long story: find() returns a cursor. By default, the cursor will transfer the results to the client in batches. From the documentation,:

For most queries, the first batch returns 101 documents or just enough documents to exceed 1 megabyte. Subsequent batch size is 4 megabytes.

Using limit() the cursor will not need to retrieve more documents than necessary. Thus reducing bandwidth consumption and latency.

Please notice that, given your use case, you will probably use a sort() operation as well. From the same documentation as above:

For queries that include a sort operation without an index, the server must load all the documents in memory to perform the sort before returning any results.

And the sort() documentation page explains further:

If MongoDB cannot obtain the sort order via an index scan, then MongoDB uses a top-k sort algorithm. This algorithm buffers the first k results (or last, depending on the sort order) seen so far by the underlying index or collection access. If at any point the memory footprint of these k results exceeds 32 megabytes, the query will fail1.


1That 32 MB limitation is not specific to sort using a limit() clause. Any sort whose order cannot be obtained from an index will suffer from the same limitation. However, with a plain sort the server need to hold all documents in its memory to sort them. With a limited sort, it only have to store k documents in memory at the same time.

like image 68
Sylvain Leroux Avatar answered Sep 17 '25 07:09

Sylvain Leroux