DynamoDB查询中已消耗读取容量单位(RCU)的计算及场景疑问
Great question! Let’s break this down clearly, since the key here is understanding the core difference between Scan and Query operations in DynamoDB (I’m assuming we’re talking about DynamoDB given the RCU terminology you referenced).
First, let’s recap the basics you already noted:
- 1 Read Capacity Unit (RCU) covers a 4KB data item for strongly consistent reads (or 2 items for eventually consistent reads).
- A Scan operation reads every item in the table, so for 50 items, it consumes 50 RCUs (assuming each item is ≤4KB).
Now, for your 10×5 table structure—let’s assume this means 10 distinct partition keys, each with 5 sort-key items under it—whether a Query consumes 50 RCUs depends entirely on what your Query is targeting:
- If you query a single partition key: The Query will only read the 5 items associated with that partition. This means it will consume 5 RCUs, not 50. Query operations are built to target specific subsets of your data via the primary key, so they don’t waste capacity reading unrelated items.
- If you need to retrieve all 50 items via Query: You’d have to run 10 separate Queries (one for each partition key) or iterate through all partitions. The total RCU consumption across all these operations would add up to 50, but each individual Query only uses RCUs for the items it actually reads. Unlike a Scan, you won’t burn through all 50 RCUs in one single operation.
- Edge case: Query with a filter matching all items: If your Query’s filter (important note: filters don’t reduce RCU usage!) matches every item, and the primary key logic requires reading all 50 items first, then yes, it would consume 50 RCUs. But this is rare and defeats the purpose of using Query over Scan.
One critical detail to keep in mind: Filters applied in a Query don’t lower RCU usage—DynamoDB still reads all items that match the primary key condition first, then applies the filter. So RCU is calculated based on the items read before filtering, not the ones returned.
In short: A Query’s RCU consumption depends on how many items it actually reads (based on primary key conditions), not the total number of items in the table. It will only consume 50 RCUs if it reads all 50 items, which is not the default or typical use case for a Query.
内容的提问来源于stack exchange,提问作者zuba




