- Newest
- Most votes
- Most comments
I'll show you two alternate options (below). A few things before we dive into them.
First, looking at your schema, each item has about 5 attributes, so I expect your items are average 40B-50B each? With that assumption, we know 1RCU can read 4K data (or 8K if you are okay with eventual consistency). At 1RCU per 4K, you can read, say, 100 items with 1 RCU. If on an average, each of your queries will return 100 items, then you can go with your current schema, and filter out the items you don't need.
Second, instead of duplicating the items by implementing that logic in your application code, you can leverage GSIs. See option#2 below.
With that, please review the following. Your business use case and shape of your data will drive which option works best for you.
==Alternate option#1==
Table-PK | Table-SK | GSI-PK | GSI-SK | Price
abc | xyz | TYPE | USA#NY#NY#PRICE | $x
(1) Filter by product type
GSI-PK = TYPE
(2) Filter by product type and location
GSI-PK = TYPE, GSI-SK starts with USA
GSI-PK = TYPE, GSI-SK starts with USA#NY
GSI-PK = TYPE, GSI-SK starts with USA#NY#NY
(3) Filter by product type, location and price
GSI-PK = TYPE, GSI-SK starts with USA, filter (price)
GSI-PK = TYPE, GSI-SK starts with USA#NY, filter (price)
GSI-PK = TYPE, GSI-SK > USA#NY#NY#PRICE
==Alternate option#2==
Table-PK | Table-SK | GSI-PK | GSI1-SK | GSI2-SK | GSI3-SK
abc | xyz | TYPE | USA#PRICE | USA#NY#PRICE | USA#NY#NY#PRICE
(1) Filter by product type
GSI-PK = TYPE
(2) Filter by product type and location
GSI-PK = TYPE, GSI3-SK starts with USA
GSI-PK = TYPE, GSI3-SK starts with USA#NY
GSI-PK = TYPE, GSI3-SK starts with USA#NY#NY
(3) Filter by product type, location and price
GSI-PK = TYPE, GSI1-SK > USA#PRICE
GSI-PK = TYPE, GSI2-SK > USA#NY#PRICE
GSI-PK = TYPE, GSI3-SK > USA#NY#NY#PRICE
Thank you so much for your answer @padma-aws it was very helpful.
You are right about the cost of filtering and I'll definitely consider it for other use cases in my app.
However, I find the second solution much better as it also allows me to sort by price (expensive/cheap first) and the first one doesn't.
Relevant content
- asked 2 months ago
- asked a year ago
- AWS OFFICIALUpdated 9 months ago
- AWS OFFICIALUpdated a year ago
- AWS OFFICIALUpdated 2 years ago
- AWS OFFICIALUpdated 2 years ago