且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在 DynamoDB 上检索以指定文本开头的列的所有项目

更新时间:2023-11-26 19:16:58

DynamoDB 目前的 Query 操作不直接支持您描述的用例 - DynamoDB 通常要求您指定一个 hashkey,然后相应地查询 range 键.

The use case you described is not directly supported by DynamoDB's Query operation today - DynamoDB typically requires you to specify a hashkey then query on the range key accordingly.

但是,有一种流行的分散收集技术,通常用于您的用例.在这种情况下,您将添加一个属性 bucket_id 并创建一个全局二级索引,其中 bucket_id 作为哈希键,Name 作为范围键.

However, there is a popular scatter-gather technique that is commonly used for usecase such as yours. In this case, you would add an attribute bucket_id and create a global secondary index with bucket_id as hash key, and Name as the range key.

bucket_id 指的是固定范围的 ID 或数字,具有足够的基数以确保您的全局二级索引分布良好.例如,bucket_id 的范围可以从 0 到 99.然后在更新基表时,每当添加新条目时,都会为其分配一个介于 0 到 99 之间的随机 bucket_id.

The bucket_id refers to a fixed range of IDs or numbers, with enough cardinality to ensure your global secondary index is well-distributed. For instance, bucket_id could range from 0 to 99. Then when updating your base table, whenever a new entry is added, a random bucket_id between 0 and 99 is assigned to it.

在您的自动完成查询期间,应用程序将为每个 bucket_id 值(0 到 99)发送 100 个单独的查询(分散),并在范围键名称上使用 BEGINS_WITH.检索结果后,应用程序必须合并 100 组响应并根据需要重新排序(收集).

During your autocomplete query, the application would send 100 separate queries (scatter) for each bucket_id value (0 to 99) and use BEGINS_WITH on the range key Name. After the results are retrieved, the application would have to combine the 100 sets of responses and re-sort as necessary (gather).

上述过程可能看起来有点繁琐,但通过确保负载均匀分布在固定键范围内,它可以让您的系统/表很好地扩展.您可以酌情增加 bucket_id 范围.为了节省成本,您可以选择将 KEYS_ONLY 投影到您的全局二级索引上,从而最大限度地降低查询成本.

The above process may seem a bit cumbersome, but it allows your system/table to scale well by ensuring the load is evenly distributed over a fixed key range. You can increase the bucket_id range as appropriate. To save cost, you can choose to project KEYS_ONLY onto your global secondary index, so cost of querying is minimized.