DynamoDB Range-Key bit-wise search

0

I have a problem where I have a Building and the building can be in multiple projects. I need to query all the buildings that belong to a specific project.

I tried to do something like this but contains cannot be used to search for the flags.

PK => company#BUILDINGS SK => buildingId#INFO LSI => flags

I know that:

  • I can do it by duplicating the building record adding the project in the range key.
  • Local Secondary Index per project cannot solve this as the number of projects is grater then 5
  • Global Secondary Index can solve this ( but having many global secondary index ( one per project ) would increase writes cost for each building update ( the cost is not that big of a issue, but still have a limit of 20, more than needed at the moment, but I wouldn't like to have that type of hard limit and would prefer to duplicate the records instead )

This could be easily ( from users query perspective ) be solved using:

  • Numeric range key and bit-wise to search for the project.
  • String range key and contains instead of only begins_with
  • String range key with pattern * something like Range Value = "01100" and search for begins_with "??1"

I'm new to dynamoDB, and I'm migrating a mongoDB project to it. So I may be missing something.

is there a easy way to solve this query problem that I'm unaware of?

thanks in advance

  • Would have project as partition key and buildings as sort keys help?

Fazel
asked a year ago247 views
1 Answer
3
Accepted Answer

You need to understand DynamoDB by first understanding its underlying data structure, which can be likened to a B+ Tree. DynamoDB is not like MongoDB in the fact it does not allow you to create access patterns which are in-efficient. All of your requests must locate items which are stored physically together, which is why contains is not a valid function on a sort key. You must use begins_with or between etc.. as you can be guaranteed those items are close together. This ensures DynamoDB holds one of its key tenets, "Predictable performance at any scale".

Querying all buildings that belong to a specific project is as simple as creating a GSI with project as the partition key.

SELECT building FROM mytable.myindex WHERE project = '123'

This would return you a list of buildings for that project.

profile pictureAWS
EXPERT
answered a year ago
profile picture
EXPERT
reviewed a month ago
  • Thanks for the reply,

    knowing it's a B+ Tree helps a lot, thanks. That explains why it lacks query capabilities in exchange for performance. But the query you gave don't help that much, as I need 1 GSI per project as one building record can be in multiple projects.

    Maybe extra records for projects??

    something like PK => companyId#Project SK => buildings field1 => buildingId Extrafields => Other Project specific info

    and querying for the main building record for extra info, duplicating the normally used to reduce the extra queries

  • Thats exactly it, you want to vertically split the items having one per building attached to the project. Use the sort key to achieve a hierarchy.

You are not logged in. Log in to post an answer.

A good answer clearly answers the question and provides constructive feedback and encourages professional growth in the question asker.

Guidelines for Answering Questions