Amazon DynamoDB | Local secondary indexes Flashcards

1
Q

Will there be parallel scan support for indexes?

Local secondary indexes

Amazon DynamoDB | Database

A

Yes, parallel scan will be supported for indexes and the semantics are the same as that for the main table.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What are local secondary indexes?

Local secondary indexes

Amazon DynamoDB | Database

A

Local secondary indexes enable some common queries to run more quickly and cost-efficiently, that would otherwise require retrieving a large number of items and then filtering the results. It means your applications can rely on more flexible queries based on a wider range of attributes.

Before the launch of local secondary indexes, if you wanted to find specific items within a partition (items that share the same partition key), DynamoDB would have fetched all objects that share a single partition key, and filter the results accordingly. For instance, consider an e-commerce application that stores customer order data in a DynamoDB table with partition-sort schema of customer id-order timestamp. Without LSI, to find an answer to the question “Display all orders made by Customer X with shipping date in the past 30 days, sorted by shipping date”, you had to use the Query API to retrieve all the objects under the partition key “X”, sort the results by shipment date and then filter out older records.

With local secondary indexes, we are simplifying this experience. Now, you can create an index on “shipping date” attribute and execute this query efficiently and just retieve only the necessary items. This significantly reduces the latency and cost of your queries as you will retrieve only items that meet your specific criteria. Moreover, it also simplifies the programming model for your application as you no longer have to write customer logic to filter the results. We call this new secondary index a ‘local’ secondary index because it is used along with the partition key and hence allows you to search locally within a partition key bucket. So while previously you could only search using the partition key and the sort key, now you can also search using a secondary index in place of the sort key, thus expanding the number of attributes that can be used for queries which can be conducted efficiently.

Redundant copies of data attributes are copied into the local secondary indexes you define. These attributes include the table partition and sort key, plus the alternate sort key you define. You can also redundantly store other data attributes in the local secondary index, in order to access those other attributes without having to access the table itself.

Local secondary indexes are not appropriate for every application. They introduce some constraints on the volume of data you can store within a single partition key value. For more information, see the FAQ items below about item collections.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are Projections?

Local secondary indexes

Amazon DynamoDB | Database

A

The set of attributes that is copied into a local secondary index is called a projection. The projection determines the attributes that you will be able to retrieve with the most efficiency. When you query a local secondary index, Amazon DynamoDB can access any of the projected attributes, with the same performance characteristics as if those attributes were in a table of their own. If you need to retrieve any attributes that are not projected, Amazon DynamoDB will automatically fetch those attributes from the table.

When you define a local secondary index, you need to specify the attributes that will be projected into the index. At a minimum, each index entry consists of: (1) the table partition key value, (2) an attribute to serve as the index sort key, and (3) the table sort key value.

Beyond the minimum, you can also choose a user-specified list of other non-key attributes to project into the index. You can even choose to project all attributes into the index, in which case the index replicates the same data as the table itself, but the data is organized by the alternate sort key you specify.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

How can I create a LSI?

Local secondary indexes

Amazon DynamoDB | Database

A

You need to create a LSI at the time of table creation. It can’t currently be added later on. To create an LSI, specify the following two parameters:

Indexed Sort key – the attribute that will be indexed and queried on.

Projected Attributes – the list of attributes from the table that will be copied directly into the local secondary index, so they can be returned more quickly without fetching data from the primary index, which contains all the items of the table. Without projected attributes, local secondary index contains only primary and secondary index keys.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What is the consistency model for LSI?

Local secondary indexes

Amazon DynamoDB | Database

A

Local secondary indexes are updated automatically when the primary index is updated. Similar to reads from a primary index, LSI supports both strong and eventually consistent read options.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Do local secondary indexes contain references to all items in the table?

Local secondary indexes

Amazon DynamoDB | Database

A

No, not necessarily. Local secondary indexes only reference those items that contain the indexed sort key specified for that LSI. DynamoDB’s flexible schema means that not all items will necessarily contain all attributes.

This means local secondary index can be sparsely populated, compared with the primary index. Because local secondary indexes are sparse, they are efficient to support queries on attributes that are uncommon.

For example, in the Orders example described above, a customer may have some additional attributes in an item that are included only if the order is canceled (such as CanceledDateTime, CanceledReason). For queries related to canceled items, an local secondary index on either of these attributes would be efficient since the only items referenced in the index would be those that had these attributes present.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

How do I query local secondary indexes?

Local secondary indexes

Amazon DynamoDB | Database

A

Local secondary indexes can only be queried via the Query API.

To query a local secondary index, explicitly reference the index in addition to the name of the table you’d like to query. You must specify the index partition attribute name and value. You can optionally specify a condition against the index key sort attribute.

Your query can retrieve non-projected attributes stored in the primary index by performing a table fetch operation, with a cost of additional read capacity units.

Both strongly consistent and eventually consistent reads are supported for query using local secondary index.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

How do I create local secondary indexes?

Local secondary indexes

Amazon DynamoDB | Database

A

Local secondary indexes must be defined at time of table creation. The primary index of the table must use a partition-sort composite key.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

Can I add local secondary indexes to an existing table?

Local secondary indexes

Amazon DynamoDB | Database

A

No, it’s not possible to add local secondary indexes to existing tables at this time. We are working on adding this capability and will be releasing it in the future. When you create a table with local secondary index, you may decide to create local secondary index for future use by defining a sort key element that is currently not used. Since local secondary index are sparse, this index costs nothing until you decide to use it.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How many local secondary indexes can I create on one table?

Local secondary indexes

Amazon DynamoDB | Database

A

Each table can have up to five local secondary indexes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How many projected non-key attributes can I create on one table?

Local secondary indexes

Amazon DynamoDB | Database

A

Each table can have up to 20 projected non-key attributes, in total across all local secondary indexes within the table. Each index may also specifify that all non-key attributes from the primary index are projected.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Can I modify the index once it is created?

Local secondary indexes

Amazon DynamoDB | Database

A

No, an index cannot be modified once it is created. We are working to add this capability in the future.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

Can I delete local secondary indexes?

Local secondary indexes

Amazon DynamoDB | Database

A

No, local secondary indexes cannot be removed from a table once they are created at this time. Of course, they are deleted if you also decide to delete the entire table. We are working on adding this capability and will be releasing it in the future.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

How do local secondary indexes consume provisioned capacity?

Local secondary indexes

Amazon DynamoDB | Database

A

You don’t need to explicitly provision capacity for a local secondary index. It consumes provisioned capacity as part of the table with which it is associated.

Reads from LSIs and writes to tables with LSIs consume capacity by the standard formula of 1 unit per 1KB of data, with the following differences:

When writes contain data that are relevant to one or more local secondary indexes, those writes are mirrored to the appropriate local secondary indexes. In these cases, write capacity will be consumed for the table itself, and additional write capacity will be consumed for each relevant LSI.

Updates that overwrite an existing item can result in two operations– delete and insert – and thereby consume extra units of write capacity per 1KB of data.

When a read query requests attributes that are not projected into the LSI, DynamoDB will fetch those attributes from the primary index. This implicit GetItem request consumes one read capacity unit per 4KB of item data fetched.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q

How much storage will local secondary indexes consume?

Local secondary indexes

Amazon DynamoDB | Database

A

Local secondary indexes consume storage for the attribute name and value of each LSI’s primary and index keys, for all projected non-key attributes, plus 100 bytes per item reflected in the LSI.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

What data types can be indexed?

Local secondary indexes

Amazon DynamoDB | Database

A

All scalar data types (Number, String, Binary) can be used for the sort key element of the local secondary index key. Set types cannot be used.

17
Q

What data types can be projected into a local secondary index?

Local secondary indexes

Amazon DynamoDB | Database

A

All data types (including set types) can be projected into a local secondary index.

18
Q

What are item collections and how are they related to LSI?

Local secondary indexes

Amazon DynamoDB | Database

A

In Amazon DynamoDB, an item collection is any group of items that have the same partition key, across a table and all of its local secondary indexes. Traditional partitioned (or sharded) relational database systems call these shards or partitions, referring to all database items or rows stored under a partition key.

Item collections are automatically created and maintained for every table that includes local secondary indexes. DynamoDB stores each item collection within a single disk partition.

19
Q

Are there limits on the size of an item collection?

Local secondary indexes

Amazon DynamoDB | Database

A

Every item collection in Amazon DynamoDB is subject to a maximum size limit of 10 gigabytes. For any distinct partition key value, the sum of the item sizes in the table plus the sum of the item sizes across all of that table’s local secondary indexes must not exceed 10 GB.

The 10 GB limit for item collections does not apply to tables without local secondary indexes; only tables that have one or more local secondary indexes are affected.

Although individual item collections are limited in size, the storage size of an overall table with local secondary indexes is not limited. The total size of an indexed table in Amazon DynamoDB is effectively unlimited, provided the total storage size (table and indexes) for any one partition key value does not exceed the 10 GB threshold.

20
Q

How can I track the size of an item collection?

Local secondary indexes

Amazon DynamoDB | Database

A

DynamoDB’s write APIs (PutItem, UpdateItem, DeleteItem, and BatchWriteItem) include an option, which allows the API response to include an estimate of the relevant item collection’s size. This estimate includes lower and upper size estimate for the data in a particular item collection, measured in gigabytes.

We recommend that you instrument your application to monitor the sizes of your item collections. Your applications should examine the API responses regarding item collection size, and log an error message whenever an item collection exceeds a user-defined limit (8 GB, for example). This would provide an early warning system, letting you know that an item collection is growing larger, but giving you enough time to do something about it.

21
Q

What if I exceed the 10GB limit for an item collection?

Local secondary indexes

Amazon DynamoDB | Database

A

If a particular item collection exceeds the 10GB limit, then you will not be able to write new items, or increase the size of existing items, for that particular partition key. Read and write operations that shrink the size of the item collection are still allowed. Other item collections in the table are not affected.

To address this problem , you can remove items or reduce item sizes in the collection that has exceeded 10GB. Alternatively, you can introduce new items under a new partition key value to work around this problem. If your table includes historical data that is infrequently accessed, consider archiving the historical data to Amazon S3, Amazon Glacier or another data store.

22
Q

How can I scan a local secondary index?

Local secondary indexes

Amazon DynamoDB | Database

A

To scan a local secondary index, explicitly reference the index in addition to the name of the table you’d like to scan. You must specify the index partition attribute name and value. You can optionally specify a condition against the index key sort attribute.

Your scan can retrieve non-projected attributes stored in the primary index by performing a table fetch operation, with a cost of additional read capacity units.

23
Q

Will a Scan on a local secondary index allow me to specify non-projected attributes to be returned in the result set?

Local secondary indexes

Amazon DynamoDB | Database

A

Scan on local secondary indexes will support fetching of non-projected attributes.