【问题标题】:Fetch data for a date range in dynamoDB在 dynamoDB 中获取日期范围的数据
【发布时间】:2020-08-08 19:37:51
【问题描述】:

我正在使用 Serverless 和 DynamoDB,并且对它比较陌生。我的应用程序有一个名为 Trips 的表。表格的参数是{id, route, cost, sell, type, date, LR, asset }和一堆其他不相关的单据编号,其中id是由uuid生成的。

根据this guide for ordering data in a date range(见最后表中的第5行)https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-modeling-nosql-B.html我添加了一个名为createdAt的参数,它是一个从1到20的随机整数,由前端发送和用HASH keySORT key 作为date 创建了一个GSI

我同时运行了 20 个 promise,以保持 createdAt 从 1 到 20。我只是将所有结果合并到一个对象中,就像这样。

await Promise.all(promises).then(function (values) {
      console.log(values);
      values.map((value) => {
        value.map((v) => {
          tripdata.push(v);
        });
      });
    });

这是回复

 [
 [
{
  Cost: 12128,
  date: '2020-04-01',
  RouteShortCode: 'Hazira-Manjusar-Sudeep',
  Selling: 11000,
  Type: 'Export'
},
{
  Cost: 12581,
  date: '2020-04-24',
  RouteShortCode: 'Hazira-Nandesari-Kevin',
  Selling: 10000,
  Type: 'Export'
}
],
[
{
  Cost: 12691,
  date: '2020-04-09',
  RouteShortCode: 'Hazira-Nandesari-Kevin',
  Selling: 10000,
  Type: 'Export'
}
],
[
{
  Cost: 11536,
  date: '2020-04-09',
  RouteShortCode: 'Hazira-Nandesari-Omega',
  Selling: 29000,
  Type: 'Import'
},
{
  Cost: 8973.5,
  date: '2020-04-18',
  RouteShortCode: 'Hazira-Manjusar-Sudeep',
  Selling: 11000,
  Type: 'Export'
}
],
[
{
  Cost: 11665,
  date: '2020-04-20',
  RouteShortCode: 'Hazira-Nandesari-Kevin',
  Selling: 10000,
  Type: 'Export'
}
],
]

tripsTable 的 Serverless.yml

tripTable:
  Type: "AWS::DynamoDB::Table"
  Properties:
    AttributeDefinitions:
      [
        { "AttributeName": "id", "AttributeType": "S" },
        { "AttributeName": "date", "AttributeType": "S" },
        { "AttributeName": "createdAt", "AttributeType": "N" },
        { "AttributeName": "Asset", "AttributeType": "S" },
      ]
    # { "AttributeName": "Route", "AttributeType": "S" },
    KeySchema:
      [
        { "AttributeName": "date", "KeyType": "HASH" },
        { "AttributeName": "id", "KeyType": "RANGE" },
      ]
    ProvisionedThroughput:
      ReadCapacityUnits: 5
      WriteCapacityUnits: 5
    StreamSpecification:
      StreamViewType: "NEW_AND_OLD_IMAGES"
    TableName: ${self:provider.environment.TRIPS}
    GlobalSecondaryIndexes:
      - IndexName: DateVSTrips
        KeySchema:
          - AttributeName: createdAt
            KeyType: HASH
          - AttributeName: date
            KeyType: RANGE
        Projection:

          ProjectionType: "ALL"
        ProvisionedThroughput:
          ReadCapacityUnits: "5"
          WriteCapacityUnits: "5"

    LocalSecondaryIndexes:
      - IndexName: TripsVSRoutes
        KeySchema:
          - AttributeName: date
            KeyType: HASH
          - AttributeName: Asset
            KeyType: RANGE
        Projection:
          ProjectionType: ALL

但问题是 2020/04/01 和 2020/04/24 上的条目具有相同的 createdAt 参数,因此在组合所有数组结果后,最终结果不按日期排序。

我需要重新排序这个列表还是我在这里遗漏了什么?如果我需要重新排序,它会不会变得更加低效?

【问题讨论】:

  • 是否有效,或者您有问题?
  • @RichardRublev 否,最终结果未按日期排序,这会产生问题,因为我需要该数据用于图形。我已添加回复,请检查
  • 向我们展示您的 serverelss.yml。
  • @RichardRublev 添加
  • 您要按date 对结果进行排序吗?还是createdAt?请告诉我们queryscan

标签: amazon-web-services database-design nosql amazon-dynamodb serverless


【解决方案1】:

DDB 将无法以排序列表的形式将数据返回给您,因为您正在执行 20 个单独的并行查询。

当您获得全部 20 个结果后,您需要自己对整个列表进行排序。

话虽如此,你真的有你正在使用的结构所需的体积吗?

您链接到的页面有以下内容

例如,假设您期望以下内容:

系统中将有多达 200 万个订单,2018 年将增长到 300 万 5 年。

这些订单中有多达 20% 将处于 OPEN 状态 时间。

平均订单记录在 100 字节左右,包含三个 OrderItem 订单分区中每个大约 50 个字节的记录,给出 您的平均订单实体大小为 250 字节。

对于该表,N 因子计算看起来像 关注。

ItemsPerRCU = 4KB / 250B = 16

PartitionMaxReadRate = 3K * 16 = 48K

N = (0.2 * 3M) / 48K = 13

如果您已经运行计算并四舍五入到 20,那么很好。

【讨论】:

  • 对不起,我没有做计算,我会做的。拥有这种结构所需的体积是什么意思?我还担心如果这 20 个查询的结果是数千个并且所有这些都需要排序会发生什么。这会是性能问题吗?
  • 容量是指多少读/写活动?这基本上是在完成计算。是的,如果你拉回足够多的记录,你会在某个时候看到一个性能问题,试图对它们进行排序......
  • 那你建议在这里做什么?如何进行更改以适应此功能?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-02-23
  • 1970-01-01
  • 1970-01-01
  • 2017-11-18
  • 1970-01-01
  • 2019-12-16
相关资源
最近更新 更多