Index projection types (ALL, KEYS_ONLY, INCLUDE) in DynamoDB - Time & Space Complexity
When using DynamoDB indexes, how fast queries run depends on what data the index holds.
We want to see how the choice of projection type affects query speed as data grows.
Analyze the time complexity of querying a Global Secondary Index with different projection types.
QueryRequest queryRequest = new QueryRequest()
.withTableName("Orders")
.withIndexName("CustomerIndex")
.withKeyConditionExpression("CustomerId = :cid")
.withExpressionAttributeValues(Map.of(":cid", new AttributeValue().withS("123")));
QueryResult result = dynamoDB.query(queryRequest);
This code queries an index that projects data using ALL, KEYS_ONLY, or INCLUDE types.
Look at what happens repeatedly during the query.
- Primary operation: Reading each matching item's projected attributes from the index.
- How many times: Once per matching item returned by the query.
As more items match the query, more data is read from the index.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | Read 10 items with projected attributes |
| 100 | Read 100 items with projected attributes |
| 1000 | Read 1000 items with projected attributes |
Pattern observation: The time grows linearly with the number of matching items.
Time Complexity: O(n)
This means the query time grows directly with how many items the index returns.
[X] Wrong: "Choosing ALL projection always makes queries faster because all data is in the index."
[OK] Correct: Reading more data per item can slow queries, so ALL projection may increase read time compared to KEYS_ONLY or INCLUDE.
Understanding how index projection affects query speed helps you design efficient databases and answer questions about scaling data access.
"What if the index projection changed from KEYS_ONLY to INCLUDE with several attributes? How would the time complexity change when querying many items?"
Practice
all attributes from the base table to the index?Solution
Step 1: Understand projection types
ALL copies every attribute from the base table to the index.Step 2: Compare with other types
KEYS_ONLY copies only keys; INCLUDE copies keys plus selected attributes.Final Answer:
ALL -> Option AQuick Check:
ALL = copies all attributes [OK]
- Confusing INCLUDE with ALL
- Thinking KEYS_ONLY copies all attributes
- Assuming PARTIAL is a valid projection type
Solution
Step 1: Recall correct syntax for KEYS_ONLY
The projection type must be exactly "KEYS_ONLY" in quotes.Step 2: Eliminate incorrect options
ALL and INCLUDE are valid but not KEYS_ONLY; "KEYS" is invalid syntax.Final Answer:
"ProjectionType": "KEYS_ONLY" -> Option BQuick Check:
KEYS_ONLY syntax = "KEYS_ONLY" [OK]
- Using "KEYS" instead of "KEYS_ONLY"
- Confusing INCLUDE with KEYS_ONLY syntax
- Omitting quotes around projection type
id, name, age, and city, and a Global Secondary Index (GSI) with projection type INCLUDE and non-key attributes age and city, which attributes will be available in the index?Solution
Step 1: Identify keys and included attributes
Index always includes keys; INCLUDE adds specified non-key attributes.Step 2: Determine which attributes are keys
Assumingidis the partition key,nameis not a key.Step 3: Combine keys and included attributes
Index hasid(key), plusageandcity(included).Final Answer:
id,age, andcity-> Option AQuick Check:
INCLUDE = keys + chosen attributes [OK]
- Including non-key attributes not specified in INCLUDE
- Assuming all attributes are included
- Confusing keys with non-key attributes
Solution
Step 1: Understand INCLUDE projection requirements
INCLUDE requires specifying at least one non-key attribute.Step 2: Check behavior when no attributes specified
Omitting non-key attributes causes a validation error during index creation.Final Answer:
The index creation will fail with a validation error. -> Option DQuick Check:
INCLUDE needs attributes; missing causes error [OK]
- Assuming INCLUDE defaults to ALL attributes
- Thinking index silently defaults to KEYS_ONLY
- Ignoring validation errors on index creation
email and retrieve phone and address attributes quickly. Which projection type should you choose?Solution
Step 1: Understand storage cost vs attribute needs
ALL copies all attributes but costs more storage; KEYS_ONLY copies only keys.Step 2: Identify needed attributes for queries
You needemailas key andphone,addressfor quick retrieval.Step 3: Choose INCLUDE projection with needed attributes
INCLUDE lets you copy keys plus onlyphoneandaddress, saving storage.Final Answer:
INCLUDE withphoneandaddress-> Option CQuick Check:
INCLUDE = keys + needed attributes saves cost [OK]
- Choosing ALL and increasing storage unnecessarily
- Using KEYS_ONLY and missing needed attributes
- Including only email but not phone or address
Ready to prove it?
Pass the test and this pattern moves unknown
