Problem: I want to get all items with highest range key.
Example:
id | version |
---|---|
test1 | 3 |
test1 | 4 |
test2 | 2 |
test2 | 3 |
I expect from query:
id | version |
---|---|
test1 | 4 |
test2 | 3 |
My table is like:
id string -> hash key
version number -> range key
I’m using guregu and I’m scanning all results to get highest version number.
var results []map[string]interface{}
err := d.table.Scan().All(&results)
What is the best practice to find items with the highest sort key ?
2
Answers
You will need to query instead and by setting
ScanIndexForward
tofalse
to get it in the reverse orderIf you changed your data model slightly where you have a record such as
V0
in the below image, you could index sparsely on the latest value allowing you to easily get the the latest version for all items in an efficient manner.The alternative is to Scan then Query which is extremely inefficient. So if it’s a frequent lookup pattern, an index is best.