If I created the following NoSQL table using DynamoDB, how might I query for the item with the largest ID
value whose Unused
value is true
without scanning the entire table?
(I have zero experience with cloud platforms and NoSQL.)
+--------+------------+-----------+
| ID | Unused | Token |
+--------+------------+-----------+
| 1 | True | ... |
+--------+------------+-----------+
| 2 | True | ... |
+--------+------------+-----------+
| 3 | True | ... |
+--------+------------+-----------+
| 4 | False | ... |
+--------+------------+-----------+
| 5 | False | ... |
+--------+------------+-----------+
You create a Global Secondary Index with Unused
as the partition key and Id
as the sort key.
Unused | ID | Token |
---|---|---|
True | 1 | --- |
True | 2 | --- |
True | 3 | --- |
False | 4 | --- |
False | 5 | --- |
Using a Query you can do the following:
SELECT * FROM mytable.myindex WHERE Unused='True' LIMIT 1 DESC
This blog will help you to understand the pros and cons.