[英]DynamoDB table design for business hours
I am trying to create a business hours application using DynamoDB
.我正在尝试使用
DynamoDB
创建营业时间应用程序。
I saw lots of examples and schema designs for different databases but just can't find the right table design for DynamoDB
.我看到了很多针对不同数据库的示例和模式设计,但就是找不到适合
DynamoDB
的表设计。
Here are my requirements :这是我的要求:
Those are the operations I need to allow:这些是我需要允许的操作:
What I've tried:我试过的:
Defined a table where business id is the partition key
( HASH
) and special dates / day of week is the sort key
( RANGE
).定义了一个表,其中业务 ID 是
partition key
( HASH
),特殊日期/星期几是sort key
( RANGE
)。
The problem with this approach is that I cannot query by multiple business hours unless I use the scan
api which is not recommended due to expensive operations.这种方法的问题是我无法按多个营业时间进行查询,除非我使用
scan
api,由于操作昂贵,因此不推荐这样做。
Please advice what kind of table design I should use for this application.请建议我应该为这个应用程序使用什么样的表格设计。
You probably need to first construct your overarching logic outside of DynamoDB, do decide if a business is working or not, and only use quarries in Dynamo for a subset of that logic.您可能需要首先在 DynamoDB 之外构建总体逻辑,确定业务是否正常运行,并且仅使用 Dynamo 中的采石场来处理该逻辑的一个子集。
Lets say though we use DynamoDB for querying in regards to normal working hours, and not include logic like holidays and special cases, you can use that to filter after you access Dynamo.假设我们使用 DynamoDB 查询正常工作时间,不包括假期和特殊情况等逻辑,但您可以在访问 Dynamo 后使用它进行过滤。 You can't construct one query in Dynamo to answer all your questions that is more like what you can do in SQL.
您无法在 Dynamo 中构造一个查询来回答您所有的问题,这更像是您在 SQL 中可以做的事情。
So lets say we have a Table/Subset of values which relate to the normal working day.因此,假设我们有一个与正常工作日相关的值表/子集。 So you have something like this:
所以你有这样的事情:
Partition Key (PK): business
, Range Key (RK): dayOfWeek
, and attributes, opens
& closes
.分区键 (PK):
business
,范围键 (RK): dayOfWeek
和属性, opens
& closes
。
We can then create 2 GSIs:然后我们可以创建 2 个 GSI:
dayOfWeek
RK opens
dayOfWeek
RK opens
dayOfWeek
RK closes
dayOfWeek
RK closes
Now we can do two queries if a store is open between 3-4pm on Monday:如果一家商店在周一下午 3 点到 4 点之间营业,现在我们可以做两个查询:
And collect only the values which appear in both queries.并仅收集出现在两个查询中的值。
Obviously though, having a PK of day, is probably not a great idea, as you will only have 7 partitions.显然,拥有一天的 PK 可能不是一个好主意,因为您将只有 7 个分区。 So what do you do?
所以你会怎么做? Well you probably have more criteria in your query than simply day, for example, the type of store, the city the store is located it, etc. That would mean then you would have a PK of something like:
city-category-dayOfWeek
.好吧,您的查询中可能有更多标准,而不仅仅是日期,例如,商店类型、商店所在的城市等。这意味着您将拥有类似以下内容的 PK:
city-category-dayOfWeek
。
Similarly on the sorting side, you might want higher rated stores to be the first option, so you might have something like: {rating}-{open}
& {rating}-{closes}
.类似地,在排序方面,您可能希望评分较高的商店成为第一个选项,因此您可能有类似的内容:
{rating}-{open}
& {rating}-{closes}
。
You will just have to get creative, firstly layout all the queries you have before you design your tables.您只需要发挥创意,首先在设计表格之前对所有查询进行布局。 I really like this video on table design, it's terrific.
我真的很喜欢这个关于桌子设计的视频,太棒了。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.