DynamoDB filter items not contains - amazon-dynamodb

I have the following problem:
Partition key (pk) and Sort key (sk):
pk sk
1 ITEMS#1
1 ORDERS#1
2 ITEMS#1
3 ITEMS#2
How can I retrieve all pk's that does not contain orders? I have tried the filter:
sk not contains "ORDERS"
But that returns
pk sk
1 ITEMS#1
2 ITEMS#1
3 ITEMS#2
Where I only want to return pk 2 and 3.

To do this efficiently, you'll have to pre-materialize the orders-or-not fact into your data and structure things in such a way that the fact is appropriately indexed and ready to use.
For example, you can create an item under each PK with an SK of META for metadata about that PK, and on that item you can have an attribute of HasOrders that's present if it has any orders in that item collection. Then you can create a GSI using that as the GSI partition key and very efficiently find all items that have orders with a Query.
You'll have to update the META every time someone places their first order.

Related

Query composite index query in Dynamo

I have a DynamoDB table with following keys:
id: partition key
created_at: sort key
brand#category#size#color: partition key for global index 'byAttributes'
The global index partition key is a composite of 4 table attributes using '#' as a delimiter.
Is there a way in DynamoDB that I can query the table using only a subset of the attributes using a wildcard for unspecified attributes?
As examples:
byAttributes = 'levis#shirts#*#red'
byAttributes = '*#pants#L#*'
I don't wish to use a FilterExpression because it only filters data after a search. I want to take advantage of the attributes being indexed.
No. But you can create alternative GSIs for different combinations.
You can also include a hierarchical SK value and use begins-with to limit based on zero or more values.
Putting some values in the PK and the rest in a hierarchical SK achieves a lot of combinations.
For example have a GSI:
PK = category,
SK = size#brand#color
Now you can query by category, category/size, category/size/brand, or all four.
If it gets more than four you may want to look at Rockset as an indexing system against DynamoDB data.

Read most recent records in DynamoDB

I have a table in DynamoDB with the following structure:
UserID (String - Partition key)
AsOfTimestamp (number - sort Key)
Product (string) ... and some other attributes.
I also have an LSI defined like this:
UserID (String - Partition Key)
Product (String - Sort Key)
AsOfTimestamp (Number) ... and other attributes
My problem is, I need to read the most recent records for all Products for a given UserID. For example, a return sample from this query could be:
UserID
Product
AsOfDate
User1
Product1
1617816274
User1
Product2
1617816288
My issue is, if I use the index on the table I can get the latest records but that does not guarantee that I will get records for each product, I could get for example a lot of records User1-Product1 before I see the most recent User1-Product2.
If I use the LSI, I can get records for a given UserId sorted by product but I will be forced to sort the results by AsOfDate and read the entire result set to get the most recent ones.
Thanks!
If I understand what you're asking for I think you're going to need another record for that data. What you want is a single AsOfDate for each user/product combination, where the AsOfDate is the most recent. If you add a record that is keyed on UserID and Product then you'll have exactly one record for each. To make that work you'll likely need to change your table structure to support the single table design pattern (or store this data in a different table). In a single table design you might have something like this:
pk
sk
UserID
Product
AsOfDate
User1
Purchase|Product1|1617816274
User1
Product1
1617816274
User1
MostRecent|Product1
User1
Product1
1617816274
User1
Purchase|Product2|1617816274
User1
Product2
1617816274
User1
Purchase|Product2|1617816288
User1
Product2
1617816288
User1
MostRecent|Product2
User1
Product2
1617816288
Then, to get all the most recent records you query for pk = userId and begins_with(sk, 'MostRecent|'). To get the other records you query for pk = userId and begins_with(sk, 'Purchase|'). Your access pattern requirements might have you changing that some, but the idea should be similar to this.
Whenever you do a new "Purchase" you would insert the new row for that, and update the "MostRecent" row as well (you can do that in a transaction if you need to, or use a DynamoDB stream to do that update).

Query DynamoDB table to fetch top 100 records order by created date

I have Dynamodb table whose primary key is partition key. This table has "created date" column also. I want to query this table to fetch top 100 records in descending order by created date. Query must have -key-condition-expression which will accept primary key of this table and it will always return maximum single record. How can I write to fetch top 100 records?
Have you read the documentation for Dynamo?
It's not an RDBMS...
In order to get more than 1 record out of DDB, you need to have a composite key (hash key + sort key)
Without knowing anything else, it sounds like you'll need to create a global secondary index with a composite key where "created date" is the sort key.
Picking the hash key for your GSI is the harder question...

Does sortkey field is mandatory as part of search criteria while retrieving data from DynamoDB?

I'm trying to run DynamoDB queries on a table containing HashKey and SortKey fields.
While doing get-item operation, when I provide key with both hashkey, sortkey fields I'm seeing the results. But when I tried with only hashkey field getting the following Exception:
An error occurred (ValidationException) when calling the GetItem operation: One of the required keys was not given a value
Can't we get DynamoDB data based on hashkey only?
When we use both partition key and sort key for a table, the primary key generated based on both. Which means there could be multiple items with the same partition key (hash key).
For an example,
partition key | sort key
1 | A
1 | B
2 | A
2 | B
In order to perform get operation, you need to specify the primary key of the item.
In your table, the hash key is not the primary key. You need to specify both partition key and sort key to get an item from the table.
Further, if you are using query operation then specifying only partition key will work.

Indexes in axapta

Hiii,
I am new to axapta
I have a table in which I want two fields together should act as a primary key. Is this possible or not
If not is there any alternate way to achieve.
There are many examples in the AOT.
For example, table MarkupTrans has primary index TableRecIdIdx created from 3 fields (TransRecId, TransTableId and LineNum); table VendInvoiceInfoLine has primary index TableRefIdx created from 5 fields (TableRefId, ParmId, OrigPurchId, LineNum and RecId), etc.

Resources