Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I make a sparse index if the key is always required?

I'm terribly confused by something in DynamoDB:

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GuidelinesForLSI.html#GuidelinesForLSI.SparseIndexes

For any item in a table, DynamoDB will only write a corresponding index entry if the index sort key value is present in the item. If the sort key does not appear in every table item, the index is said to be sparse.

[...]

To track open orders, you can create an index on CustomerId (partition key) and IsOpen (sort key). Only those orders in the table with IsOpen defined will appear in the index.

But if you have an LSI defined with an alternate sort key, when you create new items, that alternate sort key can never be null. So therefore the index isn't sparse at all because every item I create will wind up in the index.

What am I missing?

like image 963
ffxsam Avatar asked Feb 02 '18 05:02

ffxsam


1 Answers

So I finally figured it out. I should've clarified I was using the AWS console.

I created a GSI as a test, with partition key Gpart and sort key Gsort. I noticed when I create a new item, these fields are added automatically, and I cannot leave them blank, which is where I was getting stuck. (I'd get the error "One or more parameter values were invalid: An AttributeValue may not contain an empty string")

enter image description here

Turns out, all I needed to do was actually just remove those fields.

enter image description here

After removing any GSI or LSI related attributes, I can save the item, and sure enough, those indexes only show items where those keys exist.

like image 186
ffxsam Avatar answered Sep 29 '22 07:09

ffxsam