Muk*_*han 3 amazon-dynamodb dynamodb-queries
比方说,我们遇到的情况不是获取表中的总计数,而是获取具有特定状态的记录计数。我们知道 DynamoDb 是无模式的,仍然需要对每条记录进行一一计数才能获得总计数。然而,我们如何使用 dynamoDb 查询来满足上述需求?
While normally "Query" or "Scan" requests return all the matching items, you can pass the Select=COUNT parameter and ask to retrieve only the number of matching items, instead of the actual items. But before you go doing that, there are a few things you should know:
DynamoDB will still be reading - and you will still be paying for - all the data, even if just for being counted. Doing a "Scan" with a filter is in almost all cases out of the question, because it will read the entire data set every time. With a "Query" you can ask to read just one partition, or a contiguous range of sort-keys in one partition, which in some cases may be reasonable enough (but please think if it is, in your use case).
Even if you're not actually reading the data, and just counting, DynamoDB still does Scan and Query with "paging", i.e., your reads request will read just 1MB of data from disk, return you the partial count, and ask you to submit another request to resume the scan. Your DynamoDB library probably has a way to automate this resumption, so for example it can run thousands or whatever number of queries needed until finally finishing the scan and calculating the total sum.
在某些情况下,除了数据之外还维护一个计数器可能是有意义的。写入会更昂贵(例如,每次写入都会添加数据并递增计数器),但需要此计数器的读取会便宜得多 - 因此这一切都取决于每个工作负载需要多少。
| 归档时间: |
|
| 查看次数: |
5908 次 |
| 最近记录: |