简体   繁体   English

DynamoDB 排序键,以便我可以按日期查询最后一项

[英]DynamoDB Sort key so that I could query the last item by date

The following DynamoDB table has a partition key equal to date so I could easily query day by day.以下 DynamoDB 表的分区键等于日期,因此我可以轻松地逐日查询。 I know items with the same partition key are stored in the same partition and are ordered by their sort key.我知道具有相同分区键的项目存储在同一分区中,并按其排序键排序。

However, the sort key in this case is wrong because TransferId is not something like a date that I could sort.但是,这种情况下的排序键是错误的,因为TransferId不是我可以排序的日期。

What sort key should I use so I could query the last item for a specific date?我应该使用什么排序键才能查询特定日期的最后一项? 'Exchange:Time'? “交换:时间”?

+----------+------------------------------------------+---------+--------------------------------------------+------------+----------+------------+---------+-----------+---------------------------+----------------+--------------------------------------------------------------------+----------------------------------+--------------+------------------------------+
|    PK    |                    SK                    | Account |                  Address                   | AddressTag | Exchange | Instrument | Network | Quantity  |           Time            | TransactionFee |                           TransactionId                            |            TransferId            | TransferType |          UpdatedAt           |
+----------+------------------------------------------+---------+--------------------------------------------+------------+----------+------------+---------+-----------+---------------------------+----------------+--------------------------------------------------------------------+----------------------------------+--------------+------------------------------+
| 20221217 | Binance:1b56485f6a3446c3b883f4f485039260 | main    | 0xF76d3f20bF155681b0b983bFC3ea5fe43A2A6E3c | null       | Binance  | USDT       | ETH     | 97.500139 | 2022-12-17T14:59:12+00:00 |            3.2 | 0x46d28f7d0e1e5b1d074a65dcfbb9d90b3bcdc7e6fca6b1f1f7abb5ab219feb24 | 1b56485f6a3446c3b883f4f485039260 |            0 | 2023-01-27T18:00:00.5930186Z |
| 20221217 | Binance:4747f6ecc74f4dd8a4b565e0f15bcf79 | main    | 0xF76d3f20bF155681b0b983bFC3ea5fe43A2A6E3c | null       | Binance  | USDT       | ETH     | 3107.4889 | 2022-12-17T15:38:23+00:00 |            3.2 | 0xbb2b92030b988a0184ba02e2e754b7a7f0f963c496c4e3473509c6fe6b54a41d | 4747f6ecc74f4dd8a4b565e0f15bcf79 |            0 | 2023-01-27T18:00:00.2944534Z |
+----------+------------------------------------------+---------+--------------------------------------------+------------+----------+------------+---------+-----------+---------------------------+----------------+--------------------------------------------------------------------+----------------------------------+--------------+------------------------------+

I suppose the code should be something like the following:我想代码应该是这样的:

var date = DateTimeOffset.UtcNow.ToString("yyyyMMdd");

var request = new QueryRequest
{
    TableName = "transfers-dev",
    KeyConditionExpression = "#date = :date",
    ExpressionAttributeNames = new Dictionary<string, string> { { "#date", "PK" } },
    ExpressionAttributeValues = new Dictionary<string, AttributeValue> { { ":date", new AttributeValue { S = date } } },
    ScanIndexForward = false,
    Limit = 1
};
var response = await client.QueryAsync(request);

return response.Items.First();

Date as a partition key is not very useful, it essentially caps your throughput to 1000 WCU and also doesn't give your query flexibility like give me the data for the last 18 hours .日期作为分区键不是很有用,它基本上将您的吞吐量限制在 1000 WCU 并且也没有给您的查询灵活性,比如give me the data for the last 18 hours

You should choose something useful as partition key, and choose date as your sort key.您应该选择有用的东西作为分区键,并选择日期作为您的排序键。

PK PK SK SK data数据 gs_pk gs_pk
Binance:1b56485f6a3446c3b883f4f485039260币安:1b56485f6a3446c3b883f4f485039260 2022-12-17T14:59:12+00:00 2022-12-17T14:59:12+00:00 data数据 1 1个
Binance:1b56485f6a3446c3b883f4f485039260币安:1b56485f6a3446c3b883f4f485039260 2022-12-18T14:59:12+00:00 2022-12-18T14:59:12+00:00 data数据 1 1个
Binance:1b56485f6a3446c3b883f4f485039260币安:1b56485f6a3446c3b883f4f485039260 2022-12-19T14:59:12+00:00 2022-12-19T14:59:12+00:00 data数据 1 1个

You can now say give me all the data for a given binance in a given time frame.你现在可以说给我给定时间范围内给定币种的所有数据。 If you require a lookup where you want all the binances for the last given amount of time you can create a GSI and use a random or static value as partition key.如果您需要查找上次给定时间内的所有二进制数据,您可以创建一个 GSI 并使用随机值或 static 值作为分区键。 They more keys you use the more throughput you will be capable of consuming.您使用的密钥越多,您能够消耗的吞吐量就越大。

gsi_pk gsi_pk SK SK PK PK data数据
1 1个 2022-12-17T14:59:12+00:00 2022-12-17T14:59:12+00:00 Binance:1b56485f6a3446c3b883f4f485039260币安:1b56485f6a3446c3b883f4f485039260 data数据
1 1个 2022-12-17T14:59:12+00:00 2022-12-17T14:59:12+00:00 Binance:1b56485f6a3446c3b883f4f485039260币安:1b56485f6a3446c3b883f4f485039260 data数据
1 1个 2022-01-26T14:59:12+00:00 2022-01-26T14:59:12+00:00 Binance:1b56485f6a3446c3b883f4f485039260币安:1b56485f6a3446c3b883f4f485039260 data数据

Get most recent data:获取最新数据:

SELECT * FROM MYTABLE.MYINDEX WHERE gsi_pk = 1 DESC

Get data for a given day:获取给定日期的数据:

SELECT * FROM MYTABLE.MYINDEX WHERE gsi_pk = 1 AND SK BEGINS_WITH '26.01.2022'

Note笔记

Having a single value for your GSI partition key will also limit your scalability, if you intend on writing more than 1000 items per second (assuming items < 1KB) then you will have to shard the index key. GSI 分区键只有一个值也会限制您的可伸缩性,如果您打算每秒写入超过 1000 个项目(假设项目 < 1KB),那么您将不得不对索引键进行分片。

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-indexes-gsi-sharding.html https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/bp-indexes-gsi-sharding.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM