Implementing New Partition and Sort Keys for DynamoDB Table

Configuring New Partition and Sort Keys for DynamoDB Table

Prev Question Next Question

Question

Your team created a DynamoDB table called “Global-Temperature” to track the highest/lowest temperatures of cities in different countries.

The items in the table were identified by a partition key (CountryId) and there was no sort key.

The application is recently improved with more features and some queries need to be based on a new partition key (CityId) and sort key (HighestTemperature)

How should you implement this?

Answers

Explanations

Click on the arrows to vote for the correct answer

A. B. C. D.

Correct Answer - C.

Commonly, applications might need to perform various queries via different attributes as query criteria.

In this case, more global secondary indexes are required.

Please refer to https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html for its introductions.

Option A is incorrect: Because Local Secondary Index can only be configured when the DynamoDB table is created, which means a local secondary index cannot be added to an existing table.

Option Bis incorrect: Because the existing primary index cannot be modified.

In this scenario, Global Secondary Index should be considered.

Option C is CORRECT: Because Global Secondary Index can achieve this requirement.

For example:

Option D is incorrect: Because the Global Secondary index key schema can be different from the base table schema.

Even if the table contains a simple primary key (partition key), the global secondary index can still contain a composite primary key (partition key and sort key)

This is clearly illustrated in.

https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.html.
Set deployment options

Accounts
Identify accounts or organizational units in which you want to modify stacks

Deployment locations
StackSets can be deployed into accounts or an organizational unit.

Deploy stacks in accounts | | © Deploy stacks in organizational units

Organization numbers
Enter an organization unit

Z

‘ou-' followed by from 4 to 32 lower-case letters or digits (the ID of the root that contains the OU) followed by a second ‘-' dash and from 8 to 32 additional lower-case letters or
digits.

The correct answer is C. Add a Global Secondary Index with a partition key as CityId and a sort key as HighestTemperature.

Explanation: DynamoDB is a highly scalable, NoSQL database service provided by AWS. In DynamoDB, data is organized into tables, and each table consists of one or more items. An item is a collection of attributes that describe an entity. Each item in a table is uniquely identified by a primary key, which can be of two types:

  • Partition key: A simple primary key that uniquely identifies each item in the table.
  • Composite primary key: A primary key that consists of a partition key and a sort key. Together, the partition key and sort key uniquely identify each item in the table.

In this scenario, the existing DynamoDB table "Global-Temperature" uses CountryId as the partition key, and there is no sort key. However, some queries now require searching for records based on CityId and HighestTemperature.

To support these queries, we need to add a new Global Secondary Index (GSI) with CityId as the partition key and HighestTemperature as the sort key. A GSI is an independent index with its own partition key and sort key that can be used to support fast queries on non-primary key attributes. A GSI can be created at any time after a table is created, and it can be modified or deleted later.

The other options are not correct because:

A. Adding a Local Secondary Index (LSI) with CityId as the partition key and HighestTemperature as the sort key would not work because a Local Secondary Index is a secondary index that has the same partition key as the table, but a different sort key. In this case, the table's partition key is CountryId, so we cannot add a Local Secondary Index with CityId as the partition key.

B. Modifying the existing primary index with CityId as the partition key and HighestTemperature as the sort key is not possible because a primary key cannot be modified after the table is created.

D. Adding two Global Secondary Indexes, one with CityId as the partition key and another with HighestTemperature as the partition key, is not necessary and would not work because a GSI can only have one partition key. Additionally, creating two GSIs would increase the cost and complexity of the database.