alwaysAStudent
alwaysAStudent

Reputation: 2274

Index on a Boolean attribute in DynamoDB

I am new to DynamoDB schema designing. We have a table that stores metadata information for a customer with HashKey being CustomerId. The table also includes an attribute called "isActive" which is not a boolean. If customer unregisters, we plan to set the 'isActive' attribute to be empty.

We wish to pull list of all customerIds that are active. I read about 'sparseIndexes' wherein we can create a GSI on the 'isActive' attribute and only records with 'non-empty' values will be populated in the GSI.

However, it appears scanning is the only way to retrieve list of active customerIds. We can either

  a) Scan entire table and filter only active customerIds at application layer
  b) Scan the GSI which will be smaller than base table, but not necessarily very small (I would expect at least 1000+ records in it).

Are there any better design approaches to solve this by achieving high cardinality?

Upvotes: 1

Views: 2667

Answers (1)

Seth Geoghegan
Seth Geoghegan

Reputation: 5747

Sounds like you have a fairly good understanding of your options. Using GSIs to create a sparse index is fairly common for the access pattern you describe. Keep in mind that you can run a query operation against the index (as opposed to a scan), which will make the operation very fast. In the event you have many items, you could always paginate through the results.

Keep in mind you can add/remove the GSI Primary Key for the item to include/exclude the item from the index. For example, lets say your table has a GSI with a Partition (Hash) key named GSI1PK. Here's what it could look like with 4 customer items defined:

enter image description here

Notice that only Joe and Jill have a GSI1PK value defined, while Sue and Sam do not. Since I defined a global secondary index on GSI1PK, only items with that attribute defined will get projected into that index. Logically, that index would look like this:

enter image description here

If you want to remove Joe or Jill from GSI1, simply update the item to REMOVE GSI1PK from those items. Likewise, if you want to add Sue or Sam to the index, update the item to ADD the GSI1PK attribute to those items.

Upvotes: 4

Related Questions