简体   繁体   中英

DynamoDB table design for business hours

I am trying to create a business hours application using DynamoDB .

I saw lots of examples and schema designs for different databases but just can't find the right table design for 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)
  2. every day can have multiple work durations (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.
  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

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 ).

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.

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.

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. You can't construct one query in Dynamo to answer all your questions that is more like what you can do in 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 .

We can then create 2 GSIs:

  • PK dayOfWeek RK opens
  • PK dayOfWeek RK closes

Now we can do two queries if a store is open between 3-4pm on Monday:

  • PK == MONDAY & opens < 2 pm
  • PK == MONDAY & closes > 4 pm

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. 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 .

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} .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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