0Pricing
AWS for Backend Developers (EC2, S3, RDS, Lambda) · บทเรียน

การออกแบบข้อมูลใน DynamoDB

ออกแบบโครงสร้างตารางและรูปแบบการเข้าถึงที่มีประสิทธิภาพสำหรับ DynamoDB เพื่อเพิ่มประสิทธิภาพและลดค่าใช้จ่ายของแอปพลิเคชัน

การออกแบบข้อมูลใน DynamoDB เป็นบทเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) ฟรีบน CoddyKit นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส AWS for Backend Developers (EC2, S3, RDS, Lambda) มีบทเรียนทั้งหมด 4 บทเรียน

บางส่วนของบทเรียนนี้ยังไม่ได้รับการแปล และแสดงเป็นภาษาอังกฤษ

Why Data Modeling Matters for DynamoDB

DynamoDB is a NoSQL database, meaning it doesn't use traditional tables, rows, and joins like SQL. Data modeling here is all about how you'll access your data, not just how you store it.

  • Schema-less: No fixed schema, but structure is key for performance.
  • Access Patterns First: Design your tables around the queries you'll make.
  • Performance & Cost: Good modeling leads to fast queries and lower costs.

Primary Keys: Partition & Sort

Every item in DynamoDB needs a Primary Key. This key uniquely identifies each item and determines how data is stored and retrieved. It can be:

  • A Partition Key (also called a Hash Key).
  • A composite Partition Key and Sort Key (also called a Range Key).

Understanding the Partition Key (PK)

The Partition Key (PK) determines the physical partition (storage location) where your data resides.

  • Uniqueness: If only a PK is used, it must be unique for every item.
  • Distribution: A good PK distributes data evenly across partitions, preventing 'hot partitions' which can slow down performance.
  • Direct Access: You can only query an item directly if you know its Partition Key.

Leveraging the Sort Key (SK)

When you use both a Partition Key and a Sort Key (SK), items with the same Partition Key are grouped together and sorted by the Sort Key.

  • Unique Combination: The combination of PK and SK must be unique.
  • Range Queries: Enables efficient range queries (e.g., get all orders from a specific date range for a user).
  • Flexible Sorting: Allows different sorting within the same partition.

Access Patterns: Your Design Guide

Unlike relational databases where you design tables and then figure out queries, with DynamoDB, you should list all your application's data access patterns first.

  • Identify Queries: What data do you need? How will you retrieve it?
  • Examples: "Get user profile by userId", "List all products by category", "Find all comments for a postId".

Your table design (PK, SK, indexes) should directly support these patterns.

Introduction to Single-Table Design

A common and powerful DynamoDB pattern is Single-Table Design. This means storing multiple, different entity types (e.g., Users, Orders, Products) in a single table.

  • Benefits: Reduces operational overhead, enables efficient "many-to-many" relationships, and can be more cost-effective.
  • How: Uses generic attribute names like PK and SK, and prefixes (e.g., USER#<id>, ORDER#<id>) to distinguish entity types.

Querying with Global Secondary Indexes (GSIs)

What if you need to query data using an attribute that isn't part of your primary key? That's where Global Secondary Indexes (GSIs) come in.

  • New Keys: A GSI has its own Partition Key and optional Sort Key, which can be any attributes from the base table.
  • Independent: It's a completely separate table that DynamoDB maintains, allowing different access patterns.
  • Eventually Consistent: GSIs are eventually consistent, meaning changes might take a short time to propagate.

Enhancing Partitions with Local Secondary Indexes (LSIs)

Local Secondary Indexes (LSIs) allow you to query data with a different sort key within the same partition key as your base table.

  • Same PK, Different SK: LSIs share the same Partition Key as the base table but have a different Sort Key.
  • Strongly Consistent: Unlike GSIs, LSIs support strongly consistent reads.
  • Limited: Must be defined at table creation and you can have up to 5 per table.

Practical Modeling: User Posts

Let's model a simple scenario: users and their posts. We want to:

  • Access Pattern 1: Get a user's profile.
  • Access Pattern 2: Get all posts by a user, sorted by date.

Using a single table design:

  • PK: USER#<userId>
  • SK: #METADATA# (for user profile), POST#<postId> (for posts)

This allows fetching a user's profile and their posts with a single query on the USER#<userId> partition.

Quick Check: Index Types

You have a DynamoDB table storing customer orders. The primary key is customerId (Partition Key) and orderId (Sort Key).

You frequently need to query orders by orderDate for a specific customer. Which type of index would be most suitable?

Recap: Mastering DynamoDB Design

We've covered the essentials of DynamoDB data modeling:

  • Understanding how Partition Keys and Sort Keys define your data structure.
  • Designing around your access patterns, not just data relationships.
  • The power of Single-Table Design for efficiency.
  • Using Global Secondary Indexes (GSIs) for diverse queries.
  • Utilizing Local Secondary Indexes (LSIs) for alternate sorting within a partition.

Effective data modeling is crucial for optimal performance and cost in DynamoDB!

คำถามที่พบบ่อย

บทเรียน “การออกแบบข้อมูลใน DynamoDB” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การออกแบบข้อมูลใน DynamoDB” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส AWS for Backend Developers (EC2, S3, RDS, Lambda) ให้อัปเกรดเป็น CoddyKit PRO คอร์ส AWS for Backend Developers (EC2, S3, RDS, Lambda) มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การออกแบบข้อมูลใน DynamoDB”

ออกแบบโครงสร้างตารางและรูปแบบการเข้าถึงที่มีประสิทธิภาพสำหรับ DynamoDB เพื่อเพิ่มประสิทธิภาพและลดค่าใช้จ่ายของแอปพลิเคชัน คุณปฏิบัติ AWS for Backend Developers (EC2, S3, RDS, Lambda) ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน AWS for Backend Developers (EC2, S3, RDS, Lambda) บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 3 จากทั้งหมด 4 บทเรียน

บทเรียน “การออกแบบข้อมูลใน DynamoDB” ใช้เวลานานแค่ไหน

บทเรียน CoddyKit ส่วนใหญ่ใช้เวลาประมาณ 5–10 นาที แต่ละบทเรียนจึงสั้นและเป็นแบบโต้ตอบ คุณสามารถก้าวหน้าอย่างต่อเนื่องและกลับมาเรียนต่อจากตรงที่เพิ่งหยุดบนเว็บและแอปได้เลย

ฉันเขียนและรันโค้ดในบทเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) นี้ได้ไหม

ได้ บทเรียน AWS for Backend Developers (EC2, S3, RDS, Lambda) ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

บทเรียนทั้งหมดในหลักสูตรนี้

  1. รีดเรพลิกาและ Multi-AZ ของ RDS
  2. บทนำ DynamoDB
  3. การออกแบบข้อมูลใน DynamoDB
  4. สตรีม DynamoDB และตารางทั่วโลก
← กลับไปที่ AWS for Backend Developers (EC2, S3, RDS, Lambda)