简体   繁体   English

营业时间的 DynamoDB 表设计

[英]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 :这是我的要求

  1. every business should have default working hours (Monday 08:00 - 14:00, 16:00 - 20:00) and special events (26/11/2020 shop is closed / opened between 10:00 - 14:00 due to Thanksgiving)每个企业都应该有默认工作时间(星期一 08:00 - 14:00、16:00 - 20:00)和特殊活动(26/11/2020 商店因感恩节在 10:00 - 14:00 之间关闭/开放)
  2. every day can have multiple work durations (08:00 - 14:00, 16:00 - 20:00)每天可以有多个工作时间(08:00 - 14:00、16:00 - 20:00)

Those are the operations I need to allow:这些是我需要允许的操作

  1. Create / edit working hours for each business (including special events)为每个企业创建/编辑工作时间(包括特殊事件)
  2. Check whether a business (or a list of businesses) are open right now - by providing a list of business ids.检查企业(或企业列表)现在是否营业 - 通过提供企业 ID 列表。
  3. Get business (or list of businesses) working hours between 2 dates (for example 23/04/2020 - 25/04/2020) by providing a list of business ids and date range for each id通过提供业务 ID 列表和每个 ID 的日期范围,获取 2 个日期(例如 23/04/2020 - 25/04/2020)之间的业务(或业务列表)工作时间

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:

  • PK dayOfWeek RK opens PK dayOfWeek RK opens
  • PK dayOfWeek RK closes PK dayOfWeek RK closes

Now we can do two queries if a store is open between 3-4pm on Monday:如果一家商店在周一下午 3 点到 4 点之间营业,现在我们可以做两个查询:

  • PK == MONDAY & opens < 2 pm PK == 星期一 & 开放 < 下午 2 点
  • PK == MONDAY & closes > 4 pm PK == 星期一 & 关闭 > 下午 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM