"In DynamoDB, you can query by any attribute." You can filter by any attribute, but not efficiently. A Query needs the partition key. Anything else falls back to a Scan, which reads every item in the table and bills you for all of it. That is why the keys you choose on day one decide how fast and how cheap your table will be.
The partition key decides where an item lives
DynamoDB spreads a table over many internal storage partitions. When you write an item, it runs the partition key value through an internal hash function, and the result picks the partition. That is why the partition key is also called the hash attribute.
To read an item back quickly, DynamoDB needs that same value: it hashes it and goes straight to the right partition. Without it, the only option is to look everywhere.
The sort key keeps related items in order
With a composite primary key you add a sort key (the range attribute). Items that share a partition key are stored together, sorted by the sort key. Many items may share a partition key, as long as each has a different sort key.
Take an orders table with partition key customerId and sort key orderDate:
- One Query with
customerId = C7returns all of that customer's orders, already sorted. - Add a sort key condition (
between,<,>,begins_with) to get only last month's orders. - Ask for descending order to get the newest first, and add a limit to get only the latest one.
A popular pattern is a prefixed sort key such as ORDER#2026-11-02, which lets one partition hold several kinds of related items that you can select with begins_with.
Avoid hot partitions
Each partition can serve at most about 3,000 read units and 1,000 write units per second. If most of your traffic targets one partition key value, that one partition runs hot and requests get throttled, even if the table as a whole has plenty of capacity.
- Prefer keys with many distinct values that are requested fairly evenly: user IDs, device IDs, order IDs.
- Avoid low-cardinality keys such as
statusor today's date for write-heavy tables. - For an unavoidably hot key, AWS documents write sharding: add a suffix to spread writes over several key values.
Design keys around your questions
The rule of DynamoDB modelling is to start from the access patterns. List the questions your app asks ("get a customer's orders by date", "find an order by ID"), then design the keys so each question is one Query. The next episode covers secondary indexes, which add more query paths when one key is not enough.
Where AI fits
The same rule applies to AI agents. If an agent recalls memory by user, then by time, make the user ID the partition key and a timestamp the sort key. "Give me the last 20 memories for user U9" becomes one fast Query with descending order and a limit, instead of a scan that grows with every user you add.
Quick recap
- The partition key picks the place; a Query needs it.
- The sort key keeps items in order and enables range queries.
- Choose keys with many distinct values and design them around your queries.