在mongodb中的大量记录上分页速度慢 [英] Slow pagination over tons of records in mongodb

查看:1458
本文介绍了在mongodb中的大量记录上分页速度慢的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Mongo的一个馆藏中有超过30万条记录.

I have over 300k records in one collection in Mongo.

当我运行这个非常简单的查询时:

When I run this very simple query:

db.myCollection.find().limit(5);

只需要几毫秒.

但是当我在查询中使用跳过"时:

But when I use skip in the query:

db.myCollection.find().skip(200000).limit(5)

它什么也不会返回...它会运行几分钟,什么也不会返回.

It won't return anything... it runs for minutes and returns nothing.

如何使其变得更好?

推荐答案

来自MongoDB 文档:

From MongoDB documentation:

分页费用

不幸的是,跳过可能(非常)昂贵,并且要求服务器从集合或索引的开头开始才能到达偏移量/跳过位置,然后才能开始返回数据页(限制).随着页数的增加,跳过会变得更慢,并且会占用更多的CPU,并且可能会与IO绑定,并且具有更大的集合.

Unfortunately skip can be (very) costly and requires the server to walk from the beginning of the collection, or index, to get to the offset/skip position before it can start returning the page of data (limit). As the page number increases skip will become slower and more cpu intensive, and possibly IO bound, with larger collections.

基于范围的分页可以更好地使用索引,但不允许您轻松跳转到特定页面.

Range based paging provides better use of indexes but does not allow you to easily jump to a specific page.

您必须问自己一个问题:您多久需要第40000页?另请参见文章;

You have to ask yourself a question: how often do you need 40000th page? Also see this article;

这篇关于在mongodb中的大量记录上分页速度慢的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆