← 返回
数据分析

DynamoDB

Design DynamoDB tables and write efficient queries avoiding common NoSQL pitfalls.
设计 DynamoDB 表并编写高效查询,避免常见的 NoSQL 陷阱。
ivangdavila
数据分析 clawhub v1.0.0 1 版本 100000 Key: 无需
★ 2
Stars
📥 1,118
下载
💾 18
安装
1
版本
#latest

概述

Key Design

  • Partition key determines data distribution—high-cardinality keys spread load evenly
  • Hot partition = one key gets all traffic—use composite keys or add random suffix
  • Sort key enables range queries within partition—design for access patterns
  • Can't change keys after creation—model all access patterns before creating table

Query vs Scan

  • Query uses partition key + optional sort key—O(items in partition), always prefer
  • Scan reads entire table—expensive, slow, avoids indexes; almost never correct
  • "I need to filter by X" usually means missing GSI—add index, don't scan
  • FilterExpression applies AFTER read—still consumes full read capacity

Global Secondary Indexes

  • GSI = different partition/sort key—enables alternate access patterns
  • GSI is eventually consistent—writes propagate with slight delay
  • GSI consumes separate capacity—provision or pay for each GSI independently
  • Sparse index trick: only items with attribute appear in GSI

Single-Table Design

  • One table for multiple entity types—prefix partition key: USER#123, ORDER#456
  • Overloaded sort key: METADATA, ORDER#2024-01-15, ITEM#abc
  • Query returns mixed types—filter client-side or use begins_with
  • Not always right—start with access patterns, not doctrine

Pagination

  • Results capped at 1MB per request—must handle pagination
  • LastEvaluatedKey in response means more pages—pass as ExclusiveStartKey
  • Loop until LastEvaluatedKey is absent—common mistake: assume one call gets all
  • Limit limits evaluated items, not returned—still need pagination logic

Consistency

  • Reads are eventually consistent by default—may return stale data
  • ConsistentRead: true for strong consistency—costs 2x read capacity
  • GSI reads always eventually consistent—no strong consistency option
  • Write-then-read needs consistent read or retry—eventual consistency bites here

Conditional Writes

  • ConditionExpression for optimistic locking—fails if condition false
  • Prevent overwrites: attribute_not_exists(pk)
  • Version check: version = :expected then increment
  • ConditionCheckFailedException = retry with fresh data, don't just fail

Batch Operations

  • BatchWriteItem is NOT atomic—partial success possible, check UnprocessedItems
  • Retry unprocessed with exponential backoff—built into AWS SDK
  • Max 25 items per batch, 16MB total—split larger batches
  • No conditional writes in batch—use TransactWriteItems for atomicity

Transactions

  • TransactWriteItems for atomic multi-item writes—all or nothing
  • Max 100 items per transaction, 4MB total
  • TransactGetItems for consistent multi-read—snapshot isolation
  • 2x cost of normal operations—use only when atomicity required

TTL

  • Enable TTL on timestamp attribute—DynamoDB deletes expired items automatically
  • Deletion is background process—items may persist hours after expiration
  • TTL value is Unix epoch seconds—milliseconds silently fails
  • Filter attribute_exists(ttl) AND ttl > :now for queries if needed

Capacity

  • On-demand: pay per request, auto-scales—good for unpredictable traffic
  • Provisioned: set RCU/WCU, cheaper at scale—needs capacity planning
  • Provisioned with auto-scaling for predictable patterns—set min/max/target
  • ProvisionedThroughputExceededException = throttled—back off and retry

Limits

  • Item size max 400KB—store large objects in S3, reference in DynamoDB
  • Partition throughput: 3000 RCU, 1000 WCU—spread across partitions
  • Query/Scan returns max 1MB—pagination required for more
  • Attribute name max 64KB total per item—don't use long attribute names

版本历史

共 1 个版本

  • v1.0.0 当前
    2026-03-29 02:42 安全 安全

安全检测

腾讯云安全 (Keen)

安全,无风险
查看报告

腾讯云安全 (Sanbu)

安全,无风险
查看报告

🔗 相关推荐

productivity

Word / DOCX

ivangdavila
创建、检查和编辑 Microsoft Word 文档及 DOCX 文件,支持样式、编号、修订记录、表格、分节符及兼容性检查等功能。
★ 439 📥 147,745
ai-intelligence

Self-Improving + Proactive Agent

ivangdavila
自我反思+自我批评+自我学习+自组织记忆。智能体评估自身工作、发现错误并持续改进。
★ 1,358 📥 318,511
data-analysis

Stock Analysis

udiedrichsen
{"answer":"基于雅虎财经数据,分析股票与加密货币。支持投资组合管理、自选股预警、股息分析、8维评分、热门趋势扫描及传闻/早期信号探测。适用于股票分析、持仓追踪、财报异动、加密监控、热门股追踪或提前发掘非主流传闻。"}
★ 270 📥 56,992