DynamoDB - Global Secondary Index on set items

后端 未结 3 2069
遇见更好的自我
遇见更好的自我 2021-01-03 22:08

I have a dynamo table with the following attributes :

  • id (Number - primary key )
  • title (String)
  • created_at (Number - long)
  • tags (St
3条回答
  •  情话喂你
    2021-01-03 23:03

    This is a really old post, sorry to revive it, but I'd take a look at "Single Table Design"

    Basically, stop thinking about your data as structured data - embrace denormalization

    id (Number - primary key ) title (String) created_at (Number - long) tags (StringSet - contains a set of tags say android, ios, etc.,)

    Instead of a nosql table with a "header" of this:
    id|title|created_at|tags

    think of it like this:

    pk|sk    |data....
    id|id    |{title, created_at}
    id|id+tag|{id, tag} <- create one record per tag
    

    You can still return everything by querying for pk=id & sk begins with id and join the tags to the id records in your app logic

    and you can use a GSI to project id|id+tag into tag|id which will still require you to write two queries against your data to get items of a given tag (get the ids then get the items), but you won't have to duplicate your data, you wont have to scan and you'll still be able to get your items in one query when your access pattern doesn't rely on tags.

    FWIW I'd start by thinking about all of your access patterns, and from there think about how you can structure composite keys and/or GSIs

    cheers

提交回复
热议问题