Deepak
Deepak

Reputation: 1042

DynamoDB index only a specific set of values

My dynamoDB index is flooding with huge data. I would like to choose values that could be indexed and avoid indexing the rest. Is this possible?

Lets say, below are the sample items:

parent item:
{
    "hashKey":"a1"
    "indexHashKey":"parentType"
    "indexRangeKey":"date1"

}

child item:
{
    "hashKey":"a2"
    "indexHashKey":"childType"
    "indexRangeKey":"date11"

}

In my use case, I am always going to ask index to fetch only parentType records. The index is getting loaded with huge data because the childTypes are also getting indexed (and thats the nature). I would like to choose specific values (lets say 'parentType1', 'parentType2') to get indexed in dynamoDB. Is there any feature dynamoDB provides for this purpose?

Alternative: If there is no such capability dynamoDB provides, then I should either

* avoid storing the child type of the item. But it would be good to have the child type stored.

or 

* Maintain two different fields. One to store parent record type and another to store child record type. This looks ugly.

Any suggestions would be helpful.

Upvotes: 0

Views: 991

Answers (1)

readyornot
readyornot

Reputation: 2863

To be clear, you are storing both parent and child items in a single table and want an index on the table to only contain child items? Is this a correct representation of your problem?

If you do not want all the data in a DynamoDB table to be in an index, you need to design a sparse index, which is a regular index where the attributes specified for the index hash & range keys are NOT on every item in the table. Your issue is that your 'indexHashKey' and 'indexRangeKey' attributes are on ALL your parent and child items, so they are all showing up in your index. Remember, items in a DynamoDB table can have different attributes; at a minimum, they need to contain the table's hash key and sort key (if the table has one), but they do not need to contain attributes that happen to be keys for any index attached to the table.

Consider modifying your items to only include the index hash & range key attributes on your parent items. For example:

parent item:
{
    "hashKey":"a1"
    "parentIndexHashKey":"parentType"
    "parentIndexRangeKey":"date1"

}

Then you can do a query on that index by parent type (e.g. parentType == "parentType2") and return only the parent items in that table with that type.

If you also need to run a similar query for only child items, you can create a second sparse index that only has child items, by setting attributes for that index's hash and sort keys only on child items.

child item:
{
    "hashKey":"a2"
    "childIndexHashKey":"childType"
    "childIndexRangeKey":"date11"
}

Alternatively, you can store parent and child items in separate DynamoDB tables so that there is no way for child items to get into the parent index and interfere with your queries.

Upvotes: 1

Related Questions