Odpytywanie za pomocą indeksów dodatkowych: GSI i LSI
Poznaj sposób, w jaki Global i Local Secondary Indexes umożliwiają odpytywanie DynamoDB według atrybutów innych niż klucz główny, oraz dowiedz się, jak wybrać między nimi.
Odpytywanie za pomocą indeksów dodatkowych: GSI i LSI to bezpłatna lekcja Serverless Backend with AWS Lambda & API Gateway na CoddyKit. To lekcja 4 z 4. Możesz przeczytać całą lekcję poniżej za darmo — a potem ćwiczyć ją interaktywnie w przeglądarce z wbudowanym edytorem kodu i tutorem AI dostępnym 24/7. To część ścieżki edukacyjnej Serverless Backend with AWS Lambda & API Gateway, a Twój postęp synchronizuje się między webem a aplikacją CoddyKit. Kurs Serverless Backend with AWS Lambda & API Gateway zawiera 4 lekcji w sumie.
Części tej lekcji nie zostały jeszcze przetłumaczone i są wyświetlane po angielsku.
The Primary Key Limitation
DynamoDB queries efficiently only by the primary key (partition key, optionally plus sort key). To query by other attributes you need a secondary index.
- Scanning the whole table is slow and costly
- Indexes give you efficient alternate access paths
What Is a Secondary Index?
A secondary index is an alternate view of your table data, organized by a different key. DynamoDB keeps it in sync automatically as you write to the base table.
Global Secondary Index (GSI)
A GSI can use any attributes as its partition and sort key, different from the table key. It has its own throughput and can be created anytime.
- Query by a completely different attribute
- Spans all partitions of the table
- Eventually consistent reads only
Local Secondary Index (LSI)
An LSI shares the table partition key but uses a different sort key. It must be created at table creation time and supports strongly consistent reads.
GSI vs LSI at a Glance
Choosing between them:
- GSI: different partition key, created anytime, eventually consistent
- LSI: same partition key, created with the table, strongly consistent
Defining a GSI
You specify the index name and its key schema. Reads and writes against the index are billed separately from the base table.
aws dynamodb update-table --table-name Orders \
--attribute-definitions AttributeName=status,AttributeType=S \
--global-secondary-index-updates "[{...}]"Querying an Index
To query a GSI you pass its name and the index key condition. The query returns items matching that alternate key.
aws dynamodb query --table-name Orders \
--index-name status-index \
--key-condition-expression "status = :s" \
--expression-attribute-values '{":s":{"S":"PAID"}}'Projection: Which Attributes Are Copied
An index projection controls which attributes are copied into it: keys only, a selected set, or all. Projecting fewer attributes saves storage but may force a base-table fetch.
- KEYS_ONLY: smallest, just keys
- INCLUDE: keys plus chosen attributes
- ALL: full copy, biggest
Index Throughput and Cost
GSIs consume their own read and write capacity. Every base-table write that affects an indexed attribute also writes to the GSI, so over-indexing increases cost.
Sparse Indexes
If an item lacks the index key attribute, it is not written to the index. This creates a sparse index, an efficient way to query only items that have a certain attribute set.
Designing Access Patterns First
In DynamoDB you design indexes around your access patterns, not the other way around. List the queries your app needs, then create the minimal set of indexes that serve them.
Quick Check
Test your index knowledge.
Recap
You learned how secondary indexes enable querying DynamoDB by non-primary-key attributes. You compared GSIs (flexible keys, created anytime, eventually consistent) with LSIs (shared partition key, created with the table, strongly consistent), and covered projections, throughput, sparse indexes, and access-pattern-first design.
Często zadawane pytania
Czy lekcja „Odpytywanie za pomocą indeksów dodatkowych: GSI i LSI” jest bezpłatna?
Tak — pełny tekst „Odpytywanie za pomocą indeksów dodatkowych: GSI i LSI” jest dostępny za darmo tutaj w sieci. Aby ćwiczyć ją interaktywnie (wbudowany edytor kodu i tutor AI dostępny 24/7) i odblokować resztę kursu Serverless Backend with AWS Lambda & API Gateway, przejdź na CoddyKit PRO. Kurs Serverless Backend with AWS Lambda & API Gateway zawiera 4 lekcji w sumie.
Co nauczysz się w „Odpytywanie za pomocą indeksów dodatkowych: GSI i LSI”?
Poznaj sposób, w jaki Global i Local Secondary Indexes umożliwiają odpytywanie DynamoDB według atrybutów innych niż klucz główny, oraz dowiedz się, jak wybrać między nimi. Ćwiczysz Serverless Backend with AWS Lambda & API Gateway z praktycznym kodem, który uruchamiasz bezpośrednio w przeglądarce, a tutor AI dostępny 24/7 odpowiada na Twoje pytania podczas pracy nad lekcją.
Czy potrzebuję doświadczenia, aby zacząć Serverless Backend with AWS Lambda & API Gateway?
Nie wymagamy żadnego doświadczenia. Serverless Backend with AWS Lambda & API Gateway w CoddyKit jest strukturyzowany dla początkujących i zaawansowanych użytkowników, więc możesz zacząć tutaj lub od początku i uczyć się w swoim tempie. To lekcja 4 z 4.
Ile czasu zajmuje lekcja „Odpytywanie za pomocą indeksów dodatkowych: GSI i LSI”?
Większość lekcji CoddyKit trwa około 5–10 minut. Każda lekcja to mały, interaktywny krok, dzięki czemu robisz systematyczne postępy i zawsze wracasz dokładnie do tego samego miejsca — na webie i w aplikacji.
Czy mogę pisać i uruchamiać kod w tej lekcji Serverless Backend with AWS Lambda & API Gateway?
Tak. Każda lekcja Serverless Backend with AWS Lambda & API Gateway zawiera wbudowany edytor kodu, więc piszesz i uruchamiasz prawdziwy kod bezpośrednio w przeglądarce i od razu otrzymujesz sprzężenie zwrotne od AI — bez konfiguracji na komputerze.
Wszystkie lekcje w tym kursie
- Wprowadzenie do DynamoDB
- Projektowanie tabel DynamoDB
- Integracja Lambda i DynamoDB
- Odpytywanie za pomocą indeksów dodatkowych: GSI i LSI