8

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?

Community
  • 1
  • 1
ffxsam
  • 26,428
  • 32
  • 94
  • 144

3 Answers3

9

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.

ffxsam
  • 26,428
  • 32
  • 94
  • 144
0

The sort key of the table must not be null. The field you use in LSI can be null.

FelixEnescu
  • 4,664
  • 2
  • 33
  • 34
-4

Primary Key

Must have a hash key and can optionally have a sort key. The primary key must be unique.

Global Secondary Index

Must have a hash key and can optionally have a sort key. A GSI is independent of the primary key index.

Local Secondary Index

Is used to provide an alternative sort key to an existing index. The existing index by definition must already have unique keys, so the LSI does not need to have a value, hence NULL values are allowed.

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LSI.html

In a DynamoDB table, the combined partition key value and sort key value for each item must be unique. However, in a local secondary index, the sort key value does not need to be unique for a given partition key value. If there are multiple items in the local secondary index that have the same sort key value, a Query operation will return all of the items that have the same partition key value. In the response, the matching items are not returned in any particular order.

F_SO_K
  • 13,640
  • 5
  • 54
  • 83