简体   繁体   中英

DynamoDB : "Many-to-many" like relationship into a signle table

I'm trying to learn DynamoDB and i can't lie, i'm struggling to figure out best practice to make the named in relationalDB world "many-to-many relationship" between 2 tables into a single table on dynamoDb.

A simple example: A table with Drivers (driverId, name, address, skill_level) and a table with Cars (racing_id, brand, model,color, last_service_date).

A driver can drive many cars and one car can have many drivers

Example use case: Get all cars for the specific driver(cars he drives)

I know it has to be done with a GSI index, but the best i can get is only the racing_ids from all the cars.

An adjacency list is the usual way this is addressed in DynamoDB. In this case you would store your driver and your car as items in the table and then you would also store a CarDriver that would hold the relationship between the car and the driver. You use the GSI to maintain the back links so you can see either side of the relationship. Typically some (duplicate) car and driver information may need to be stored in the relationship item so that It can be viewed in the opposite view.

The tricky part is learning where to put the data and where/when to duplicate the data. A lot of this is based off the access patterns.

In your example CarDriver would probably contain The Driver's name, and the car's brand, model, and color. Typically what I would do is make an attribute called car_embed that is a map with brand, model and color as key's in the map.

PK SK GSI1PK GSI2PK Entity_Type
Driver#1 Driver#1 Driver
Driver#1 Car#1 Car#1 Driver#1 CarDriver
Driver#2 Driver#2 Driver
Driver#2 Car#1 Car#1 Driver#2 CarDriver
Driver#2 Car#2 Car#2 Driver#2 CarDriver
Car#1 Car#1 Car#1 Car#1 Car
Car#2 Car#2 Car#2 Car#2 Car

If you want to really understand this you need to read the DynamoDB Book

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