MAX operations on Amazon DynamoDB

I am new to using NoSQL databases. I have a table in DynamoDB with over 100,000 items in it. In addition, this table is frequently updated. In this table, I want to be able to do something similar to this in the world of relationship databases:

Select * from tableName where attributeName = (Select MAX(attributeName) from tableName); 

Is there an inexpensive way to do this without taking all the lines into Java code? Any inputs / pointers would be appreciated. Thanks.

+4
source share
1 answer

There is no cheap way to do this without adding space or complexity.

An expensive way would be to scan the entire table, retrieve only the key attributes and attributeName , calculate max, and then retrieve all the (full) elements that you found with this maximum value.

If you have a composite key (hash and range) and several hash keys of relative common elements in the table, local secondary indexes will help, and cost only a little space. You can have an index in the attributeName attributeName and then query for each hash key using that index, as well as "isScanIndexForward: false" and "limit: 1" to get "max" from this hashKey. Then you calculate the maximum number of all results (1 result from each hash key), and you know the value to extract. You can get them all the same, with reverse lookup and the "EQ" condition on attributeName .

If you want to add complexity, you can save this information in an additional table. Something like tableName.extraInfo that has this element: {hashKey: "maxOfAttributeName", "value":5} . Then, when you put or update in the main table, you will also update this field and you will make a difference. Beware of the transaction complexities associated with this approach if your data changes rapidly.

+2
source

Source: https://habr.com/ru/post/1496918/


All Articles