Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Uniqueness in DynamoDB secondary index

Question:

DynamoDB tables with a primary key that is a composite hash-range key are unique. Does this extend to secondary indices too?

Example:

I have a comments DynamoDB table with a post_id primary key and comment_id range key. Additionally there's a local secondary index with a date-user_id range key.

Each entry is a comment a user has left on post. The purpose of the secondary index is to count how many unique users left a comment on a post on a specific day.

Entry 1: post_id: 1 comment_id: 1 date-user_id: 2014_06_24-1

Entry 2: post_id: 1 comment_id: 2 date-user_id: 2014_06_24-1

Entry 3: post_id: 1 comment_id: 3 date-user_id: 2014_06_24-2

When I do a query specifying the secondary index, and pass in a condition of post_id equals 1 and a date-user_id equals 2014_06_24-1, I'm getting a count of 2 and I'm expecting a count of 1.

Why does the secondary index have two entries with the same primary key/range key.

like image 603
user3772393 Avatar asked Jun 24 '14 18:06

user3772393


People also ask

How do you enforce uniqueness in DynamoDB?

In DynamoDB, each record in a table is uniquely identified by the primary key for your table. You can use this primary key to ensure there is not an existing record with the same primary key. To do so, you would use a Condition Expression to prevent writing an item if an item with the same key already exists.

How many secondary indexes are allowed in DynamoDB?

Each table in DynamoDB can have up to 20 global secondary indexes (default quota) and 5 local secondary indexes. For more information about the differences between global secondary indexes and local secondary indexes, see Improving data access with secondary indexes.

How do I create a unique constraint in DynamoDB?

To define a unique constraint for the username too, just add items with the type “username”. Also, by defining the rarer of the two parts of the key as the partition, you'll end up with more partitions, which is a best practice when working with DynamoDB.

Why would you use a secondary index on a DynamoDB table?

A global secondary index lets you query over the entire table, across all partitions. A local secondary index lets you query over a single partition, as specified by the partition key value in the query. Queries on global secondary indexes support eventual consistency only.


1 Answers

Secondary indexes don't guarantee uniqueness. From the docs:

In addition, remember that global secondary indexes do not enforce uniqueness

http://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GuidelinesForTables.html

like image 200
Mike Hornblade Avatar answered Sep 21 '22 13:09

Mike Hornblade