在 mongodb 中对大量记录进行缓慢分页 [英] Slow pagination over tons of records in mongodb

查看:47
本文介绍了在 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:

传呼费用

不幸的是,skip 可能(非常)昂贵,并且需要服务器从集合或索引的开头走,到达偏移/跳过位置,然后才能开始返回数据页(限制).随着页数的增加,skip 会变得更慢,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天全站免登陆