You are working on a web application whose database was created by a DB admin who is no longer working with your company.
The manager has asked you to handle the database administration.
You are working on a new reporting feature for which you need to query a DynamoDB table, and the attributes you need to query upon are not set as Partition Key and Sort Key.
What is the best way to solve this problem?
Click on the arrows to vote for the correct answer
A. B. C. D.Correct Answer: C.
Using Global Secondary Index, you can create a different pair of Partition and Sort key even after table creation for better search performance.
Option A is incorrect: This is not the best practice as you are duplicating the data unnecessarily.
Option B is incorrect: Local Secondary Index only supports additional Sort Key and cannot be created after the table has been created.
Option D is incorrect: DynamoDB does not support this feature as of writing this question.
Reference:
https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GSI.htmlThe correct answer is B. Create a Local Secondary Index with required attributes as Partition and Sort Key.
DynamoDB is a NoSQL database, and it uses a different data model than relational databases. Tables in DynamoDB are made up of partitions, and each partition is made up of items. Each item has a primary key, which consists of a partition key and an optional sort key. Partition key is used to identify the partition where the item is stored, and sort key is used to sort items within the partition.
In DynamoDB, queries can only be performed on partition keys and sort keys. Therefore, to perform a query on attributes that are not set as Partition and Sort Key, we need to create an index.
Option A, cloning the table and changing the Partition and Sort Key during table creation, is not a feasible solution because it requires copying all the data from the original table to the new table, which is not practical for large tables. Additionally, it does not solve the problem of querying on non-Partition Key and Sort Key attributes.
Option C, creating a Global Secondary Index, is a possible solution, but it is an overkill. Global Secondary Indexes are used to query across partitions and allow for faster and more efficient querying, but they come at the cost of increased storage and write capacity requirements.
Option D, editing the table and changing the existing Partition and Sort Key to the required ones, is not possible. Once a table is created, its Partition Key and Sort Key cannot be changed.
Therefore, the best solution is to create a Local Secondary Index (LSI) with the required attributes as Partition and Sort Key. Local Secondary Indexes are indexes that are created within the same partition as the original table, and they can only be created at the time of table creation. LSI allows you to query on attributes that are not set as the table's Sort Key. However, you can only have up to five LSI per table, and they share the same provisioned throughput as the table.
In summary, Option B is the best solution to query upon attributes not set as Partition and Sort Key in DynamoDB.