[英]Sorting items from DynamoDB using dynamic value?
I have thousands of items in a table.我在一张桌子上有数千个项目。 Each item has a numeric attribute score
.每个项目都有一个数字属性score
。 What I want to do:我想做的事:
targetScore
, select all items where targetScore - n <= score <= targetScore + n
(I got this part working).给定一些targetScore
, select targetScore - n <= score <= targetScore + n
的所有项目(我让这部分工作)。j
elements where j:= ABS(targetScore - score)
in ASC order.按 ASC 顺序仅返回j:= ABS(targetScore - score)
的前j
个元素。Since the table is somewhat large, and will only grow over time, I'm hoping to have a way to delegate step #2 above to the DynamoDB engine.由于表有点大,并且只会随着时间的推移而增长,我希望有一种方法可以将上面的第 2 步委托给 DynamoDB 引擎。 I'm currently selecting all item IDs from step #1, doing the computation for step #2 in process, then selecting the resulting items on a subsequent query.我目前正在从第 1 步中选择所有项目 ID,正在为第 2 步进行计算,然后在后续查询中选择结果项目。
You don't really need step 1 to get step 2. I'm assuming that score
is a sort key in your setup.您实际上并不需要第 1 步来获得第 2 步。我假设score
是您设置中的排序键。
In step 2, you want to sort the items by abs(targetScore - score)
and take the j
lowest ones.在第 2 步中,您希望按abs(targetScore - score)
对项目进行排序并取j
最低的项目。
Although DynamoDB doesn't have any direct way to do that, the definition of the absolute value makes it quite easy to do.尽管 DynamoDB 没有任何直接的方法可以做到这一点,但绝对值的定义使它很容易做到。 First, observe that `abs(targetScore - score) is defined as follows: It is:首先,观察`abs(targetScore - score)的定义如下:它是:
targetScore - score
if score <= targetScore
targetScore - score
如果score <= targetScore
则得分score - targetScore
if score >= targetsScore
score - targetScore
如果score >= targetsScore
So the items with the smallest abs are either items with a score higher than targetScore
but as low as possible, or items with a score lower than targetScore
, but as high as possible.因此,abs 最小的项目要么是得分高于targetScore
但尽可能低的项目,要么是得分低于targetScore
但尽可能高的项目。
So, you can do two separate DynamoDB queries:因此,您可以执行两个单独的 DynamoDB 查询:
j
items with score >= targetScore
, sorted in increasing score
.查询score >= targetScore
的前j
个项目,按score
递增排序。j
items with score < targetScore
, sorted in decreasing score
.查询score < targetScore
的前j
个项目,按score
递减排序。 DynanoDB can do both queries for you efficiently and cheaply - it involves a Query
with a KeyConditions
parameter and a Limit
, and ScanIndexForward
to do decreasing sorting. DynanoDB 可以高效且廉价地为您执行这两种查询 - 它涉及一个带有KeyConditions
参数和一个Limit
的Query
以及ScanIndexForward
来进行递减排序。 But no expensive and inefficient FilterExpression
!但没有昂贵且低效FilterExpression
!
Once you have the j
smallest items of each type 1 and type 2, all that remains is to pick the overall j
smallest items from these 2*j
items we got.一旦你有了每个类型 1 和类型 2 的j
个最小的项目,剩下的就是从我们得到的这2*j
个项目中选择整个j
最小的项目。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.