Geek_To_Learn
Geek_To_Learn

Reputation: 1956

Dynamo DB Concepts

I know some of the questions I am going to be asked will be so silly, but I am new to Dynamo Db and I have a lot of confusion about it.

My questions are :

  1. After going through concept of Hash and Range key through this post What is Hash and Range Primary Key? I am thinking that is it possible to create a Range key which is not a part of Primary key. Suppose I want to define a Table Orders {**Id**,Date,Name....} with Id as a Hash Key and Date as a Range Key and Date not as a part of Primary Key.

  2. Is it possible to query a Table consisting Primary key as Hash and Range Key with only Hash key or Range key ? Like In Table orders {**ID,Date**,Address,Quantity....} say I have defined primary key as Hash and Range Key with ID as Hash Key and Date as Range key. Can we query on table using only ID or Date but not both ?

  3. What is the concept of projected attributes while creating a Local Secondary Index and Global Secondary Index ?

Upvotes: 1

Views: 585

Answers (1)

Alexander Patrikalakis
Alexander Patrikalakis

Reputation: 5205

  1. A Range key is part of a primary key. Now, a Range key might be part of the schema of the base table or an index (LSI, GSI) but it will always be part of a primary key, and will always be accompanied by a Hash key.
  2. Once you create a table, you cannot change its schema. You can only do Query on the Range key for a particular Hash key in a Hash+Range table. Therefore, to enable Queries on the Hash key given a particular Range key, you would need to create a GSI in that table that basically reverses the schema of your base table. The Hash key of the GSI would be the Range key of the base table, and the Range key of the GSI would be the hash key of the base table. Then, you can Query the GSI using the same Query API as you can already use for the base table. For example, say you have a Library table that has Hash=owner and Range=BookISBN. You can do a Query for all the book ISBNs that belong to the owner=Alex, or for a range of ISBN, but to find the book ISBNs that belong to Michelle, you would have to do a different Query call. Now, with this schema, it is difficult to find the owners of the book with ISBN=123412341234. In fact, you would have to Scan the entire table to find all the owners of the book with ISBN=123412341234. If you add a GSI called ISBNIndex to your Library table with Hash=ISBN and Range=owner, then you can use a Query call on the ISBNIndex GSI to find all the owners of a book with ISBN=123412341234.
  3. Projected attributes allow you to select the attributes you want to include in items in your indexes. Indexes can speed up data retrieval by use of alternate schemas. See the documentation for GSI and LSI. The primary keys of the base table are always included in index projections. If you select KEYS_ONLY, items in the index will only include base table and index primary keys. If you select INCLUDE, the attributes you specify in NonKeyAttributes will be included in the index in addition to the base table and index primary keys. If you select ALL, all the item attributes will be projected into the index. For more information, see the CreateTable documentation.

Upvotes: 4

Related Questions