如何在 DynamoDB 全局二级索引和总数中检索行的位置? [英] How to retrieve a row's position within a DynamoDB global secondary index and the total?

查看:20
本文介绍了如何在 DynamoDB 全局二级索引和总数中检索行的位置?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在实现一个由 DynamoDB 和他们的 Global Secondary Index 支持的排行榜,如他们的开发人员指南中所述,http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html

I'm implementing a leaderboard which is backed up by DynamoDB, and their Global Secondary Index, as described in their developer guide, http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html

但是,对于排行榜系统非常必要的两件事是您在其中的位置,以及排行榜中的总数,因此您可以显示 2000 年的第 1 名或类似情况.

But, two of the things that are very necessary for a leaderboard system is your position within it, and the total in a leaderboard, so you can show #1 of 2000, or similar.

使用索引,行以正确的方式排序,我认为这些调用足够便宜,但我还没有找到一种方法,如何通过他们的文档.我真的希望我不必每次都获取整个表来知道一个人在其中的位置,或者整个表的计数(尽管如果它不可用,那可能会被延迟、计算和存储在表).

Using the index, the rows are sorted the correct way, and I'd assume these calls would be cheap enough to make, but I haven't been able to find a way, as of yet, how to do it via their docs. I really hope I don't have to get the entire table every single time to know where a person is positioned in it, or the count of the entire table (although if that's not available, that could be delayed, calculated and stored outside of the table at scheduled periods).

我知道 DescribeTable 会为您提供有关整个表的信息,但我会将过滤器应用于范围键,因此不适合此目的.

I know DescribeTable gives you information about the entire table, but I would be applying filters to the range key, so that wouldn't suit this purpose.

推荐答案

DynamoDB 的美妙之处在于它针对非常特定(和常见)的用例进行了高度优化.这种优化的代价是许多其他用例不能像其他数据库那样容易地实现.不幸的是,你的就是其中之一.话虽如此,使用 DynamoDB 有完全有效的好方法可以做到这一点.我碰巧构建了一个与您的要求相同的应用程序.

The beauty of DynamoDB is that it is highly optimized for very specific (and common) use cases. The cost of this optimization is that many other use cases cannot be achieved as easily as with other databases. Unfortunately yours is one of them. That being said, there are perfectly valid and good ways to do this with DynamoDB. I happen to have built an application that has the same requirement as yours.

您可以做的是在您的表上启用 DynamoDB 流,并使用 Lambda 函数处理项目更新事件.每次用户的点数发生变化时,您都会重新计算他​​们的排名并更新您的项目.即使您使用相同的扫描操作重新计算排名,这仍然要好得多,因为它将大部分成本从您的读取操作转移到您的写入操作,这首先是 NoSQL 的重点.这种方法还可以让您的积分更新快速并最终保持一致(排名不会立即更新,但可以保证正确更新,除非您的 Lambda 函数出现问题).

What you can do is enable DynamoDB Streams on your table and process item update events with a Lambda function. Every time the number of points for a user changes you re-compute their rank and update your item. Even if you use the same scan operation to re-compute the rank, this is still much better, because it moves the bulk of the cost from your read operation to your write operation, which is kind of the point of NoSQL in the first place. This approach also keeps your point updates fast and eventually consistent (the rank will not update immediately, but is guaranteed to update properly unless there's an issue with your Lambda function).

我建议采用这种方法,一旦达到规模优化,就可以通过在 Redis 之类的东西中按排名缓存用户,除非您有此方面的经验并且可以快速进行设置.首先选择最简单的.如果您担心排行榜变化太频繁,您可以通过仅重新计算第一名(例如 100 个用户)的排名并安排另一个 Lambda 函数每隔几分钟运行一次、扫描所有用户并更新他们的排名来降低成本同一时间.

I recommend to go with this approach and once you reach scale optimize by caching your users by rank in something like Redis, unless you have prior experience with it and can set this up quickly. Pick whatever is simplest first. If you are concerned about your leaderboard changing too often, you can reduce the cost by only re-computing the ranks of first, say, 100 users and schedule another Lambda function to run every several minutes, scan all users and update their ranks all at the same time.

这篇关于如何在 DynamoDB 全局二级索引和总数中检索行的位置?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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