英文:
Real time max aggregation from DynamoDB?
问题
问题背景 -
我有两个DynamoDB,一个名为Shop,一个名为Product。
Shop属性 -
Shop_id(分区键)
Shop_Name
Product属性 -
Product_id(分区键)
Product_Name
Shop_Id
Price
问题 -
在Shop DynamoDB中创建一个名为max_price
的新字段,当产品的价格发生变化时,应该更新该字段。
主要问题是当价格下降时如何找到下一个最高价格。
另外,我不能在Product DynamoDB中创建一个全局二级索引(GSI)。(无法在数据库中添加新的索引,只允许添加新属性)
我考虑过的方法 -
使用最大堆和最小堆是一种维护/获取max_price
的方法,但实现堆将需要另外两个DynamoDB。
是否有更简单的方法?
英文:
Problem context -
I have 2 DynamoDB named Shop and Product.
Shop attributes -
Shop_id (partition key)
Shop_Name
Product attributes-
Product_id(partition key)
Product_Name
Shop_Id
Price
Problem -
Create a new field max_price
in Shop dynamoDB and that should be updated when price of products are updated.
The main problem is when price is decreased how to find the next max price.
Also i cannot create a GSI in Product DynamoDB. (New index cannot be added in DB, adding new attributes are only allowed)
What approaches I thought of -
Using max heap and min heap is one way to maintain/obtain max_price but implementing heaps will require 2 more dynamoDB
Is there a simpler approach ?
答案1
得分: 1
如果您绝对无法在产品表上创建索引,那么我能想到的下一个最佳选项是创建一个新的DynamoDB表,将Shop_Id
作为哈希键,Price
作为排序键,并使用DynamoDB Streams和Lambda设置与产品表之间的准实时复制。然后,您可以使用新表轻松获取最高价格,使用排序键。您还可以使用ElastiCache Redis中的排序集,而不是DynamoDB表。
英文:
If you absolutely cannot create an index on the Product table, then the next best option I can think of is to create a new DynamoDB table that has Shop_Id
as a hash key and Price
as a sort key and set up near real-time replication between Product table and this new table using DynamoDB Streams and Lambda. You can then use new table to easily get the max price using sort key. You could also use something like sorted sets in ElastiCache redis instead of a DynamoDB table.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论