简体   繁体   中英

Best way to retrieve an item from dynamoDB using attribute which is not partition key

I am new to dynamoDB and need some suggestion from experienced people here. There is a table created with below model

orderId - PartitionKey
stockId
orderDetails

and there is a new requirement to fetch all the orderIds which includes particular stockId. The item in the table looks like

{
   "orderId":"ord_12234",
   "stockId":[
      123221,
      234556,
      123231
   ],
   "orderDetails":{
      "createdDate":"",
      "dateOfDel":""
   }
}

provided the scenario that stockId can be an array of id it cant be made as GSI.Performing scan would be heavy as the table has large number of records and keeps growing. what would be the best option here, How the existing table can be modified to achieve this in efficient way

You definitely want to avoid scanning the table. One option is to modify your schema to a Single Table Design where you have order items and order/stock items.

For example:

pk sk orderDetails stockId ...
order#ord_12234 order#ord_12234 {createdDate:xxx, dateOfDel:yyy} ...
order#ord_12234 stock#123221 23221 ...
order#ord_12234 stock#234556 234556 ...
order#ord_12234 stock#123231 123231 ...

You can then issue the following queries, as needed:

  1. get the order details with a query on pk=order#ord_12234, sk=order#ord_12234
  2. get the stocks for a given order with a query on pk=order#ord_12234, sk=stock#
  3. get everything associated with the order with a query on pk=order#ord_12234

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