What are the best practices for secondary indexes and the differences between global and local secondary indexes in DynamoDB?
I want to know the differences between global secondary indexes and local secondary indexes in Amazon DynamoDB. I also want implement best practices for secondary indexes.
Resolution
Differences between global and local secondary indexes
A global secondary index is an index with a partition key and sort key that can be different from the base table. A local secondary index has the same partition key as the base table but a different sort key. To see the differences between the two indexes, see Improving data access with secondary indexes.
Best practices
Note: If you receive errors when you run AWS Command Line Interface (AWS CLI) commands, then see Troubleshoot AWS CLI errors. Also, make sure that you're using the most recent AWS CLI version.
Review your storage configurations for your indexes
To optimize the costs of your configurations, review the considerations for local secondary indexes and global secondary indexes.
Restore an entire table
When you restore a DynamoDB table and select the Restore the entire table option, all local and global secondary indexes are included. If you restore the entire table, then make sure to account for the time and space.
Add a secondary index in an existing table
To create a local secondary index for an existing table, create a new table with a local secondary index. Then, use the AWS CLI or AWS SDK to migrate the data to the destination table. For more information, see How can I migrate my DynamoDB tables from one AWS Account to another?
To create a global secondary index in an existing table, use the AWS CLI or the DynamoDB console.
Note: For existing tables, you can't remove a local secondary index. The local secondary index has the same lifetime as the base table.
DynamoDB returns an error
If you get an ItemCollectionSizeExceededException error, then an item collection exceeds the 10 GB quota. To resolve this issue, reduce the size of the collection. For more information, see the Item collection size limit.
If you get a ValidationException error, then there's a data type mismatch in your table. When you write an item to a base table, the data type must match the index key schema's data type. It's not a best practice to specify attributes for global secondary index sort keys. For more information, see Data synchronization between tables and global secondary indexes.
Don't use GetItem and BatchGetItem operation
Because indexes can contain multiple items that share the same set of keys, you can't use the GetItem and BatchGetItem operations. It's a best practice to use your indexes for queries.
Note: Because of increased costs, it's a best practice to create secondary indexes only on attributes that you frequently query.
For more best practices, see Best practices for using secondary indexes in DynamoDB.
Related information
Improving data access with secondary indexes
Take advantage of sparse indexes
Working with global secondary indexes: Java
Copy Amazon DynamoDB tables across accounts using a custom implementation
Contenuto pertinente
- AWS UFFICIALEAggiornata 2 anni fa
- AWS UFFICIALEAggiornata 2 anni fa
- AWS UFFICIALEAggiornata 3 anni fa
- AWS UFFICIALEAggiornata 2 anni fa