Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
459 views
in Technique[技术] by (71.8m points)

mongodb: will limit() increase query speed?

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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...