Dynamodb Request Operations

Is it possible to work with OR , AND queries in DynamoDB?

I need to know if DynamoDB has something like "where fname = xxxx OR lname = xxxx " from SQL queries? in Rails

Thanks.

+4
source share
4 answers

In general, no.

DynamoDB allows only efficient search using the primary (hash key), as well as an optional range request for the "range key". Other attributes are not indexed.

You can use the Scan query to read the entire table filter using a set of attributes, but this is a relatively expensive and slow option for large tables.

You can simulate AND by creating a primary key that includes both values ​​for the query, and OR by creating duplicate tables, each of which uses one attribute as its primary key and queries both tables in parallel with BatchGetItem

+6
source

As BCoates mentioned, the answer is NO.

If you need sequential reads, you cannot use BatchGetItem.

+1
source

It is not possible to use the "OR" operator, for example, in KeyConditionExpression: '#hashkey =: hk_val AND #rangekey>: rk_val',

it Uses And Operator to match keys for HOT and RANGE.

Therefore, we cannot use OR in Dynamo Db.

0
source

Although there is no such thing as OR , AND , you can still simulate SQL queries using scan and filterexpression .

But remember that if you use a scan, this means that the entire table will be extracted and then processed, and the scan does not always scan the entire table in one iteration. so you can skip some items. therefore, this method is usually avoided because it is very expensive. but here is a piece of python3 code using boto3 api that can mimic the requested sql query.

 response = table.scan( FilterExpression=Attr('fname').eq('xxxxx') | Attr('lname').eq('xxxxx')) 

filterexpression also different operators like &, ~

0
source

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


All Articles