Primary and sort key

When we create a table, we are required to select one of the attributes from our dataset as the primary key. As we have already discussed, the primary key is used to distribute the data across the cluster and to identify the item when performing CRUD operations. The primary key should be selected so that the data is easily identifiable and distributed evenly across the keys. We should always use a many-to-less relationship when choosing our primary key to ensure that the distribution of data will be as even as possible.

For example, in our DynamoDB table with our users, it would be wise to choose the username as the primary key as it is unique and it would ensure that each set of attributes for each username is evenly distributed across the cluster. Finding a user's other attributes is done by specifying the primary key, in this case the username, since DynamoDB keeps a data index that contains the primary key and its storage location.

But sometimes it is not efficient to sort the data with the key that will give us the best distribution, and for these kinds of situations we can add an optional sort key to our data. So, for example, we can use the last_name attribute as the sort key in our table, ensuring that the index is created with both the username and last_name attributes, meaning we are able to retrieve the attributes for each item by simply specifying the last name. 

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset