Skip and limit or slice?

1,299 views
Skip to first unread message

Stephen Cannon

unread,
Mar 5, 2013, 6:44:22 AM3/5/13
to mongod...@googlegroups.com
I am wondering about performance when doing pagination from a mongodb collection.

Based on this document there appear to be performance considerations if I use skip and limit.


Alternatively I could use $slice projection or just .slice the query result set and pagination that way.

What are the performance considerations and recommendations?

Ronald Stalder

unread,
Mar 31, 2013, 3:09:36 PM3/31/13
to mongod...@googlegroups.com
Hi Stephen

the $slice projection operator works on arrays within documents, not on a list of documents returned by find(). See http://docs.mongodb.org/manual/reference/projection/slice/

About the performance considerations of skip(): if you have a result set of 200k documents and ask to skip(199990), then yes, this will be 

... expensive because it requires the server to walk from the beginning of the collection or index to get the offset or skip position ...
Consider using range-based pagination for these kinds of tasks. That is, query for a range of objects, using logic within the application to determine the pagination rather than the database itself. This approach features better index utilization, if you do not need to easily jump to a specific page.



So, it depends on how big your result set is and how many documents you'll have to skip

Does this help?

Cheers
Ronald

Stephen Cannon

unread,
Apr 2, 2013, 7:29:03 AM4/2/13
to mongod...@googlegroups.com
Ronald,

Yes this does help.  I also did some very unscientific tests by pushing about 20,000 records into the browser.  $slice "appeared" to work better than skip limit.  This is merely from an end user perception.  I did not actually test/time the effects.  Though the perceptive differences were quite noticeable in terms of seconds faster for $slice over skip limit.

Thanks
S



--
--
You received this message because you are subscribed to the Google
Groups "mongodb-user" group.
To post to this group, send email to mongod...@googlegroups.com
To unsubscribe from this group, send email to
mongodb-user...@googlegroups.com
See also the IRC channel -- freenode.net#mongodb
 
---
You received this message because you are subscribed to a topic in the Google Groups "mongodb-user" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/mongodb-user/fUj4XU4x3E4/unsubscribe?hl=en.
To unsubscribe from this group and all its topics, send an email to mongodb-user...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 



--
Thanks
Stephen Cannon
Reply all
Reply to author
Forward
0 new messages