Database Reference
In-Depth Information
Hash and range primary key
Along with the hash key, DynamoDB also supports another primary key called range key,
which can be used in combination with the hash key. In this case, DynamoDB created an
unordered hash index on the hash key and sorted the range key index on a range key attrib-
ute. Range key should be selected in such a manner that it will evenly distribute the data
load across partitions.
A good example to choose a hash and range type primary key for the Person table would
be choosing birth years as the hash key and SSN as the range key. Here, we can narrow
down our search with the birth year and then search in a specific birth year range partition
for a specific SSN.
Note
In general, the hash index is a set of buckets identified by a certain hash value. The hash
value is calculated by some function that is robust and gives the same results on multiple
executions. The hashing algorithm decides what value gets emitted for a given key. The
simplest example of hash algorithms is the Modulo 10 function: f(x) = x Modulo 10 . Here,
x is a key. So, if we use this hashing algorithm for indexing and a key has a value, say 17, it
would emit the hash value as 17 Modulo 10 = 7.
Range index is a special type of index that helps align the set of data into a certain range,
which helps in faster retrieval of the given item. When we implement the range index on a
certain attribute, we create n number of slots on that data. Consider we have five slots for a
given set of data starting from key value 1 to 100, then slots get created from 1-20,
21-40,..., and 81-100.
Now let's redefine our bookstore schema and see when to use the hash key and when to use
the composite hash and range keys. We can consider the following primary key specifica-
tions for the given tables:
Primary key
type
Table name
Hash key
Range key
yearOfPublishing —this will allow us to save topics as per the
year in which they got published
bookId —unique
book ID
Book (yearOfPublish-
ing,bookId, , ..)
Hash key
and range
Author (authorId,..)
Hash key
authorId —unique author ID
NA
Search WWH ::




Custom Search