DynamoDB Secondary Indexes: GSI and LSI
18 Questions
0 Views

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson

Questions and Answers

A DynamoDB table uses customer_id as the partition key. Frequent queries require sorting customers by order_date for each customer_id. Which type of secondary index is most suitable for this scenario?

  • Local Secondary Index (LSI) with `order_date` as the sort key. (correct)
  • No secondary index is needed; use the primary key for this query.
  • Global Secondary Index (GSI) with `customer_id` as the partition key and `order_date` as the sort key.
  • Global Secondary Index (GSI) with `order_date` as the partition key.

You need to create a DynamoDB index that allows you to query your products table by category and sort the results by price. The products table uses product_id as its primary key. Which type of secondary index should you use?

  • Local Secondary Index (LSI) with `category` as the partition key and `price` as the sort key.
  • Local Secondary Index (LSI) with `product_id` as the partition key and `price` as the sort key.
  • Global Secondary Index (GSI) with `category` as the partition key and `price` as the sort key. (correct)
  • Global Secondary Index (GSI) with `product_id` as the partition key and `price` as the sort key.

Which statement accurately describes a key difference between Global Secondary Indexes (GSIs) and Local Secondary Indexes (LSIs) in DynamoDB?

  • GSIs must have the same partition key as the base table, while LSIs can have a different one.
  • LSIs can be created after the table is created, while GSIs must be defined at table creation.
  • LSIs support eventual consistency, while GSIs provide strong consistency.
  • GSIs can have a different partition key from the base table, while LSIs must have the same partition key. (correct)

A DynamoDB table named game_scores uses user_id as the partition key. You want to efficiently query and sort scores based on game_type for a specific user_id. Given the need for strong consistency, which index type is most appropriate?

<p>Local Secondary Index (LSI) because it allows sorting by <code>game_type</code> while maintaining the <code>user_id</code> partition key and supports strong consistency. (A)</p> Signup and view all the answers

You are designing a DynamoDB table to store user activity events. The table uses event_type as the partition key. You now realize the need to query events based on user_id within each event_type. You have already created 5 LSIs on the table. What is the recommended approach?

<p>Use a Global Secondary Index (GSI) with <code>user_id</code> as the partition key. (C)</p> Signup and view all the answers

Which of the following statements is true regarding the modification of secondary indexes in DynamoDB after table creation?

<p>Global Secondary Indexes (GSIs) can be modified, but Local Secondary Indexes (LSIs) cannot be modified after table creation. (D)</p> Signup and view all the answers

A transactions table stores financial transactions with transaction_id as the primary key. There's a need to efficiently query transactions by customer_id and filter by transaction_amount. Which indexing strategy would be most effective, assuming you want the index to only contain customer_id, transaction_amount and transaction_id?

<p>Create a Global Secondary Index (GSI) with <code>customer_id</code> as the partition key, <code>transaction_amount</code> as the sort key, and project only the necessary attributes. (A)</p> Signup and view all the answers

You have a DynamoDB table with millions of entries and need to perform complex queries that involve filtering and sorting on multiple non-key attributes. You anticipate high read/write traffic. What is the most important consideration when choosing between using multiple Global Secondary Indexes (GSIs) versus performing scans with filters?

<p>Using multiple GSIs can significantly increase write costs and storage requirements due to data duplication. (D)</p> Signup and view all the answers

When should you consider using a Global Secondary Index (GSI) over a Local Secondary Index (LSI) in DynamoDB?

<p>When you require a partition key that is different from the DynamoDB base table. (A)</p> Signup and view all the answers

A DynamoDB table has a partition key CustomerID and a sort key OrderDate. You need to frequently query the table to find all orders placed on a specific date for each customer. Which type of secondary index is most suitable, and why?

<p>An LSI, because it allows querying using a different sort key within the same <code>CustomerID</code> partition. (A)</p> Signup and view all the answers

Your application needs to query a DynamoDB table based on an attribute that is not the primary key. You anticipate a high volume of read requests on this index. Which factor would be MOST important when choosing between a GSI and an LSI?

<p>The read/write capacity mode, as only GSIs support both provisioned and on-demand capacity. (B)</p> Signup and view all the answers

You're designing a DynamoDB table for an e-commerce application. The table stores customer orders, and you need to support queries based on both CustomerID (partition key) and OrderDate. Furthermore, you want to enable queries based on OrderStatus (e.g., 'Shipped', 'Delivered'). Which indexing strategy offers the most flexible querying options?

<p>Using a GSI with <code>OrderStatus</code> as the partition key. (A)</p> Signup and view all the answers

When creating an LSI, which of the following statements is correct regarding its partition key?

<p>The partition key must be the same as the base table's partition key. (B)</p> Signup and view all the answers

Consider a scenario where you have a DynamoDB table named Employees with EmployeeID as the partition key. You need to frequently query employees based on their Department. Which of the following approaches is MOST efficient?

<p>Creating a GSI with <code>Department</code> as the partition key. (C)</p> Signup and view all the answers

You have a DynamoDB table with a high volume of write operations and frequent queries on a non-key attribute. Which secondary index characteristic is MOST important to consider for optimizing performance and minimizing costs?

<p>The index's partition key cardinality (number of distinct values). (D)</p> Signup and view all the answers

Which of the following is a limitation of Local Secondary Indexes (LSIs) compared to Global Secondary Indexes (GSIs) in DynamoDB?

<p>LSIs cannot be created after the table is created. (D)</p> Signup and view all the answers

Which of the following scenarios is best suited for using an LSI over a GSI?

<p>When you need to ensure strong consistency for your queries. (C)</p> Signup and view all the answers

Referring to the CLI examples provided, what is the primary difference in how a GSI and LSI are created in DynamoDB?

<p>GSIs are defined within the <code>--global-secondary-index-updates</code> parameter, while LSIs are defined within the <code>--local-secondary-indexes</code> parameter. (D)</p> Signup and view all the answers

Flashcards

Secondary Indexes

Indexes that allow querying data in ways not possible with the primary key alone.

Global Secondary Index (GSI)

Index with a partition key and optional sort key different from the table's primary key.

GSI Query Scope

Enables queries on non-primary key attributes, spanning all items in the table.

When to Use GSI

Used when you need to query data in multiple ways, using different attributes as keys.

Signup and view all the flashcards

Local Secondary Index (LSI)

Index with the same partition key as the base table but a different sort key.

Signup and view all the flashcards

LSI Use Case

Allows different sorts of queries on data, keeping the same partition key.

Signup and view all the flashcards

LSI Limit per Table

You can only have up to 5 LSIs per table.

Signup and view all the flashcards

Index Modification

GSIs can be modified after it has been created, LSIs cannot be modified after creation and must be created with the base table.

Signup and view all the flashcards

GSI Partition Key

For GSIs, this can be different from the base table's key.

Signup and view all the flashcards

LSI Partition Key

For LSIs, this must be the same as the base table's key.

Signup and view all the flashcards

GSI Capacity Mode

GSIs can use provisioned or on-demand capacity modes.

Signup and view all the flashcards

LSI Capacity Mode

LSIs are limited to provisioned capacity mode only.

Signup and view all the flashcards

GSI Consistency

DynamoDB: Indexes with eventual consistency only.

Signup and view all the flashcards

LSI Consistency

DynamoDB: Indexes with eventual and strong consistency.

Signup and view all the flashcards

Study Notes

  • Secondary indexes in Amazon DynamoDB enable querying data beyond the primary key.
  • There are two types: Global Secondary Index (GSI) and Local Secondary Index (LSI).

Global Secondary Index (GSI)

  • It facilitates creating an index with partition and sort keys different from the table's primary key.
  • Enables querying on attributes that are not part of the primary key.
  • Spans all items in a table, irrespective of the partition key.
  • Useful for querying data in multiple ways.
  • Any attribute can function as the partition key.
  • Any attribute can function as the sort key (optional).
  • Projection specifies attributes included in the index like ALL, Keys only, or INCLUDE specific attributes.
  • Supports eventual consistency.
  • Operates in both provisioned and on-demand capacity modes.
  • Can be modified after table creation.
  • An example: create a GSI with email as the partition key if the table stores users and queries by email are frequent.

Local Secondary Index (LSI)

  • It allows index creation with the same partition key as the base table but with a different sort key.
  • Useful when different sorts of queries are needed on data while keeping the same partition key.
  • The partition key must match the base table partition key.
  • Any other attribute can be the sort key.
  • Projection mirrors GSI, which specifies attributes projected.
  • Supports eventual and strong consistency.
  • Limited to a maximum of 5 LSIs per table.
  • It's created when the table created and cannot be modified afterward.
  • An example: create an LSI with registration_date as the sort key if the table has users with user_id as the partition key, and querying by registration_date is required.

Key Differences Between GSI and LSI

  • GSI's partition key can differ from the base table, but LSI's must be the same.
  • A sort key for GSI can be any attribute, while for LSI, it must differ from the base table's sort key.
  • GSI supports provisioned or on-demand capacity modes; LSI supports provisioned mode only.
  • GSI indexes are virtually unlimited, but LSI is capped at 5 per table.
  • GSI uses eventual consistency, whereas LSI supports both eventual and strong consistency.
  • GSIs can be modified after table creation, whereas LSIs can't.

Choosing Between GSI and LSI

  • Use GSI when: a completely different partition and sort key than the base table is needed.
  • Use GSI when: flexible querying pattern spanning all table items is wanted.
  • Use GSI when: an application demands scalability, low latency, and high availability.
  • Use LSI when: querying requires a different sort key, but the partition key remains the same.
  • Use LSI when: strong consistency on queries is needed.
  • Use LSI when: the data model necessitates sorting within the same partition key.

Example of Creating a GSI and LSI

  • AWS CLI can be used to create both GSI and LSI
  • The command utilizes "aws dynamodb update-table"
  • The attribute definitions specify your attribute name and type definitions
  • The key schema specifies your attribute name and key type (HASH or RANGE)
  • The Projection specifies the attributes you want projected, and the type (ALL)
  • The throughput needs to be provisioned for GSI (Read and Write Capacity Units)

Summary

  • Use GSIs for flexible queries with different partition and sort keys, spanning the entire table.
  • Use LSIs for querying within the same partition key but with different sorting, especially when strong consistency is paramount.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Description

Explore secondary indexes in Amazon DynamoDB, including Global Secondary Indexes (GSI) and Local Secondary Indexes (LSI). Learn how to use indexes with partition and sort keys, query attributes, and project specific data. Understand consistency and capacity modes.

More Like This

South Sudan Secondary School Education
40 questions
Global Perspectives IGCSE Flashcards
27 questions
Global Wind Patterns Quiz
4 questions
Use Quizgecko on...
Browser
Browser