Issue in single-table design DynamoDB and limit query parameters

huangapple go评论76阅读模式
英文:

Issue in single-table design DynamoDB and limit query parameters

问题

我想能够限制每次查询获取的提取元素数量,但我在这里遇到了以下问题:

由于我在DynamoDB中使用了单表设计,我只获取到了四个项目:

Issue in single-table design DynamoDB and limit query parameters

如果我检查DynamoDB中的前10个元素,我会注意到无论我在扫描操作中过滤了哪些项目,我都会在这里获取计数:
Issue in single-table design DynamoDB and limit query parameters

      async paginate<TEntity>(
        limit = 10,
        start_key: any
        TableName = $env.MAIN_TABLE
      ): Promise<Paginate<TEntity>> {
        Assert.ok(TableName)
        const items= await this.instance.scan({
           TableName
           Limit: limit,
           ExclusiveStartKey: start_key,
           FilterExpression: 'contains(PK, :PK)',
           ExpressionAttributeValues: {
             ':PK': 'QUIZ#'
           }
        })
        return items
      }

这里有人知道有没有解决这个问题的方法吗?

英文:

I want to be able to limit the fetch elements I am getting in each query, but I am facing the following issue here:

Since I use the single-table design in DynamoDB I am getting only four items here:

Issue in single-table design DynamoDB and limit query parameters

If I check the 10 first elements in DynamoDB, I will notice that I am getting the count here, regardless of the items I am filtering in my Scan operation
Issue in single-table design DynamoDB and limit query parameters

  async paginate&lt;TEntity&gt;(
    limit = 10,
    start_key: any
    TableName = $env.MAIN_TABLE
  ): Promise&lt;Paginate&lt;TEntity&gt;&gt; {
    Assert.ok(TableName)
    const items= await this.instance.scan({
       TableName
       Limit: limit,
       ExclusiveStartKey: start_key,
       FilterExpression: &#39;contains(PK, :PK)&#39;,
       ExpressionAttributeValues: {
         &#39;:PK&#39;: &#39;QUIZ#&#39;
       }
    })
    return items
  }

Does anyone here know a workaround for this?

答案1

得分: 1

你正在执行一个带有 Limit=10Scan 操作,同时提供了一个 FilterExpression,这意味着 DynamoDB 首先会读取前 10 个项目,然后评估 FilterExpression,并返回那批 10 个项目中评估为 True 的项目。

文档的第一行
> 单个 Scan 操作最多读取设置的项目数量(如果使用了 Limit 参数),或最多 1MB 的数据,然后使用 FilterExpression 对结果进行过滤。如果响应中存在 LastEvaluatedKey,则需要对结果集进行分页处理。有关更多信息,请参阅 Amazon DynamoDB 开发人员指南中的“分页结果”。

关键在于你需要进行分页来获取 10 个项目。

我的建议是,永远不要设计数据模型,其中需要对分区键进行包含查询。

英文:

You are doing a Scan with Limit=10 and also providing a FilterExpression which means DynamoDB will first read the first 10 items, then evaluate the FilterExpression and return which ever items in that batch of 10 evaluate to your filter as True.

First line of docs
> A single Scan operation reads up to the maximum number of items set (if using the Limit parameter) or a maximum of 1 MB of data and then apply any filtering to the results using FilterExpression. If LastEvaluatedKey is present in the response, you need to paginate the result set. For more information, see Paginating the Results in the Amazon DynamoDB Developer Guide.

The bottom line is that you will need to paginate to achieve 10 items.

My advice is that you should never model your data where you need a contains for the partition key lookup

huangapple
  • 本文由 发表于 2023年5月22日 11:18:37
  • 转载请务必保留本文链接:https://go.coder-hub.com/76302844.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定