不知道主键的 DynamoDB 查询

DynamoDB query without knowing the primary key

我有一个 table 具有 3 个属性

ARTICLE | CLAPS | STAMP
 -----  | ----- | ----
 RUST   |   1   | 2 OCT 2020 19:20
 C++    |   3   | 10 OCT 2020 12:30

我想在 table 上执行这 3 个查询:

  1. 检查文章名称是否被占用
  2. 拍手数最高的文章
  3. 最新文章

不使用扫描操作查询 2 和 3 的最有效方法是什么?

What is the most efficient way to query 2 and 3 without using a scan operation?

创建全球二级索引 (GSI)

哈希键可以是字符串“CLAPS”或字符串“STAMP”,排序键可以是相应的 CLAPS 值或 STAMP 值。

当您 Query() 时,指定“ScanIndexForward=false”and“Limit=1”` 以获得最高(最新)值。

请注意,您需要以可排序的格式存储时间戳,“2020-10-10-12:30”而不是“2020 年 10 月 10 日 12:30”

看完@Charles 的回答后。我正在采取以下方法。

TABLE

  POST | TYPE | CLAPS | STAMP
 ----- | ---- | ----- | ----
 RUST  |  A   |   1   | 1594185483964
 C++   |  A   |   3   | 1594185984082

这里的TYPE A表示一篇文章。

我制作了 2 个 GSI。

  1. 分区键类型,排序键 CLAPS ;索引:TYPE-CLPAS-POSTS
  2. 分区键类型,排序键 STAMP ;索引:TYPE-STAMP-POSTS

我认为这会增加灵活性,同时允许我进行有效查询。

  1. 找到拍手数最多的文章。
{
  TableName: 'WB360-POSTS',
  IndexName: 'TYPE-CLAPS-POSTS',
  KeyConditionExpression: '#T = :T',
  ScanIndexForward: false,
  Limit: 10,
  ExpressionAttributeNames: {
   '#T': 'TYPE'
  },
  ExpressionAttributeValues: {
   ':T': 'A'
  },
  ProjectionExpression: 'POST'
}
  1. 查找最新文章。
{
  TableName: 'WB360-POSTS',
  IndexName: 'TYPE-STAMP-POSTS',
  KeyConditionExpression: '#T = :T',
  ScanIndexForward: false,
  Limit: 10,
  ExpressionAttributeNames: {
   '#T': 'TYPE'
  },
  ExpressionAttributeValues: {
   ':T': 'A'
  },
  ProjectionExpression: 'POST'
}