Indexes help you find data faster in a database. But they use extra space and cost more to run.
Index capacity and cost in DynamoDB
No direct code syntax for capacity; capacity is managed by settings and usage.
Indexes consume read and write capacity units separately from the main table.
More indexes mean more cost and storage use.
Create a Global Secondary Index (GSI) with provisioned capacity: { "IndexName": "MyIndex", "KeySchema": [ {"AttributeName": "Category", "KeyType": "HASH"}, {"AttributeName": "CreatedDate", "KeyType": "RANGE"} ], "Projection": {"ProjectionType": "ALL"}, "ProvisionedThroughput": { "ReadCapacityUnits": 5, "WriteCapacityUnits": 2 } }
Read capacity units (RCUs) and write capacity units (WCUs) are charged separately for each index.
This command creates a table with a global secondary index. The index has its own read and write capacity units, which add to the total cost.
aws dynamodb create-table \
--table-name Music \
--attribute-definitions \
AttributeName=Artist,AttributeType=S \
AttributeName=SongTitle,AttributeType=S \
AttributeName=Genre,AttributeType=S \
--key-schema \
AttributeName=Artist,KeyType=HASH \
AttributeName=SongTitle,KeyType=RANGE \
--provisioned-throughput ReadCapacityUnits=5,WriteCapacityUnits=5 \
--global-secondary-indexes \
'[{
"IndexName": "GenreIndex",
"KeySchema": [
{"AttributeName":"Genre","KeyType":"HASH"}
],
"Projection": {"ProjectionType":"ALL"},
"ProvisionedThroughput": {
"ReadCapacityUnits": 2,
"WriteCapacityUnits": 1
}
}]'Indexes increase your storage and throughput costs because they keep copies of data.
Write operations cost more because data must be written to the table and all indexes.
Monitor your index usage to avoid paying for unused capacity.
Indexes speed up queries but use extra capacity and cost more.
Each index has its own read and write capacity units.
Plan indexes carefully to balance performance and cost.
Practice
Solution
Step 1: Understand the purpose of indexes
Indexes in DynamoDB are designed to speed up queries by providing alternate ways to access data efficiently.Step 2: Eliminate unrelated options
Options about storage size, backup, and encryption are unrelated to index functionality.Final Answer:
To speed up query performance by allowing efficient data access -> Option AQuick Check:
Indexes improve query speed = B [OK]
- Thinking indexes reduce storage cost
- Confusing indexes with backup features
- Assuming indexes encrypt data
Solution
Step 1: Identify correct GSI syntax
Global Secondary Indexes require an array under "GlobalSecondaryIndexes" with full details: IndexName, KeySchema, Projection, and ProvisionedThroughput.Step 2: Compare options
{ "GlobalSecondaryIndexes": [ { "IndexName": "MyGSI", "KeySchema": [ {"AttributeName": "UserId", "KeyType": "HASH"} ], "Projection": {"ProjectionType": "ALL"}, "ProvisionedThroughput": {"ReadCapacityUnits": 5, "WriteCapacityUnits": 5} } ] } correctly uses "GlobalSecondaryIndexes" as an array with all required fields. Others use wrong keys or incomplete structures.Final Answer:
{ "GlobalSecondaryIndexes": [ { "IndexName": "MyGSI", "KeySchema": [ {"AttributeName": "UserId", "KeyType": "HASH"} ], "Projection": {"ProjectionType": "ALL"}, "ProvisionedThroughput": {"ReadCapacityUnits": 5, "WriteCapacityUnits": 5} } ] } -> Option BQuick Check:
GSI syntax requires array with full details = A [OK]
- Using LocalSecondaryIndexes instead of GlobalSecondaryIndexes
- Providing object instead of array for GSIs
- Omitting ProvisionedThroughput for GSIs
Solution
Step 1: Understand write capacity consumption
Writing one item consumes write capacity units on the main table plus on each index that is updated.Step 2: Add main table and GSI write units
Main table uses 10 units, GSI uses 5 units, total = 10 + 5 = 15 units.Final Answer:
15 units -> Option CQuick Check:
Total write capacity = main + GSI = 15 [OK]
- Counting only main table write units
- Counting only GSI write units
- Multiplying instead of adding capacities
Solution
Step 1: Analyze write capacity usage
Writes to the table also consume write capacity on GSIs, increasing total usage.Step 2: Identify cause of exceeded capacity
If GSIs have high write capacity or many writes, they can cause capacity to be exceeded even if main table writes are low.Final Answer:
You have a Global Secondary Index with high write capacity units causing extra cost -> Option AQuick Check:
GSIs increase write capacity usage = C [OK]
- Assuming no indexes means no extra capacity
- Confusing read and write capacity units
- Thinking Local Secondary Indexes increase write capacity
Solution
Step 1: Understand cost drivers
Write capacity units on both table and GSI affect cost. Reducing GSI units lowers cost but must still handle write load.Step 2: Apply auto scaling and adjust capacity
Reducing GSI write units to match actual writes (5) and enabling auto scaling balances cost and performance.Final Answer:
Reduce the GSI write capacity units to 5 and enable auto scaling -> Option DQuick Check:
Lower GSI units + auto scaling = cost optimization [OK]
- Removing GSI and losing query performance
- Increasing table capacity unnecessarily
- Disabling write capacity causing failures
Ready to prove it?
Pass the test and this pattern moves unknown
