【问题标题】:Selecting data in Dynamodb based on values in another field根据另一个字段中的值选择 Dynamodb 中的数据
【发布时间】:2021-04-17 00:29:12
【问题描述】:

我在 DynamoDB 中有来自 Plaid API 的数据转储。每个事务都有 transaction_id、pending(bool) 和 pending_transaction_id(FK 基本上是它替换的旧的挂起事务)

{
 "account_id": "acct1", // partition key
 "transaction_id": "txn100", // sort key
 "category_id": "22001000",
 "pending": false,
 "pending_transaction_id": "txn1",
 "amount": 500,
},
{
 "account_id": "acct1",
 "transaction_id": "txn1",
 "category_id": "22001000",
 "pending": true,
 "pending_transaction_id": null,
 "amount": 500,
},

是否可以在单个查询中只查询尚未永久替换的待处理事务?

换句话说,如果它是关系型数据库,它将是这样的 select * from txn where pending == false and transaction_id not in (select pending_transaction_id from txn)(或任何你喜欢的 CTE 或左加入)。

如何在单个查询中在 dynamo db 中执行此操作?

【问题讨论】:

  • 它没有任何帮助。你应该分享dynamodb表方案,以便我们分析。
  • DynamoDB 在设计时并未考虑比较(连接)文档(行)。 stackoverflow.com/questions/36753861/…
  • @MatBailie 谢谢你,Mat,这也是我在看了无数教程后学到的。我将在代码中执行此操作或重新设计表格。谢谢

标签: amazon-dynamodb dynamodb-queries


【解决方案1】:

我们可以在这里有一个 GSI 来解决这个问题。

PK (pending) SK (pending_transaction_id) ..
false txn1 ..
true null ..

然后我们可以查询哪些PK记录并获取我们的记录。

需要考虑/观察的要点:

  1. 由于此处SK为空,因此不会创建记录。这对我们有用,因为我们不需要这些记录。
  2. 如果需要,我们可以在 GSI 中包含 pending = true 记录,但这意味着具有 "NULL" 属性值。

我在这里看到的 GSI 的优势是(仅考虑第 1 点),我们只保留我们在查询中需要的重复记录。

【讨论】:

    猜你喜欢
    • 2013-08-20
    • 1970-01-01
    • 1970-01-01
    • 2017-03-03
    • 2016-03-19
    • 1970-01-01
    • 2013-10-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多