MongoDB Academy · บทเรียน

การแบ่งพาร์ทิชันข้อมูล S3 เพื่อประสิทธิภาพการค้นหา

ผู้เรียนจะกำหนดแอตทริบิวต์พาร์ทิชันบนเส้นทาง S3 เพื่อให้ Data Federation ตัดไฟล์ที่ไม่เกี่ยวข้องออกและส่งมอบการค้นหาเชิงวิเคราะห์ที่รวดเร็ว

บทเรียน 4 จาก 413 ขั้นตอน

การแบ่งพาร์ทิชันข้อมูล S3 เพื่อประสิทธิภาพการค้นหา เป็นบทเรียน MongoDB Academy ฟรีบน CoddyKit นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน คุณสามารถอ่านบทเรียนทั้งหมดด้านล่างฟรี — จากนั้นลองปฏิบัติด้วยตัวคุณเองในเบราว์เซอร์พร้อมตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7 บทเรียนนี้เป็นส่วนหนึ่งของเส้นทางการเรียน MongoDB Academy และความก้าวหน้าของคุณจะซิงค์ข้ามเว็บและแอป CoddyKit คอร์ส MongoDB Academy มีบทเรียนทั้งหมด 4 บทเรียน

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

Why Partitioning S3 Data Matters

In Atlas Data Federation, every query against an S3-backed virtual collection potentially reads many files. Without partitioning, even a query asking for a single day's data might scan an entire year of files. Partitioning organises S3 objects into a directory structure that encodes queryable metadata in the path, allowing the query engine to skip irrelevant files — a technique called partition pruning.

Partition Pruning: The Core Mechanism

Partition pruning works because Atlas Data Federation parses the S3 object key (path) and extracts the values defined as partition attributes in the storage configuration. When a query filter matches one of these attributes, the query engine only reads objects whose path values match — skipping all others without even issuing S3 GetObject requests for them.

// Path template with partition attributes
// /events/{year int}/{month int}/{day int}/data.parquet

// Query: fetch March 15, 2025 data
db.events.find({ year: 2025, month: 3, day: 15 })

// Data Federation issues S3 ListObjects only for:
// /events/2025/3/15/
// All other years/months/days are skipped

Designing Your Partition Structure

Choose partition attributes based on how your queries filter data. Common patterns: time-based (year/month/day) for time-series data, geo-based (region/country) for global data, entity-based (tenantId, userId) for multi-tenant SaaS. Put the most-frequently-filtered partition attribute highest in the hierarchy for the greatest pruning effect.

// Time-based partition hierarchy
// /events/{year int}/{month int}/{day int}/{hour int}/

// Entity + time partition (tenant queries prune all other tenants)
// /orders/{tenantId string}/{year int}/{month int}/

// Geo + time
// /events/{region string}/{year int}/{month int}/

Registering Partition Attributes in Storage Config

Partition attributes are declared in the storage configuration's path field using curly-brace syntax with the attribute name and type: {attrName type}. Supported types are string, int, and ISODate. The type determines how the attribute is parsed and compared against query filter values.

{
  'dataSources': [{
    'storeName': 's3Store',
    'path': '/events/{year int}/{month int}/{day int}/*.parquet'
  }]
}
// Partition attributes: year (int), month (int), day (int)
// Filters like { year: 2025, month: 3 } trigger pruning
// The '*.parquet' at the end matches any file in the day directory

File Size: Finding the Sweet Spot

Partition pruning works at the directory level, but file size also matters. Too many tiny files (thousands of 1 KB files) wastes time on per-file S3 API calls, even after pruning. Too few giant files (one 100 GB file per month) prevents pruning below the month level. Aim for file sizes of 64 MB to 256 MB (compressed), with enough files per partition to enable sub-partition pruning where needed.

// Ideal partition design for daily query patterns:
// /events/2025/03/15/part-001.parquet  (~128 MB each)
// /events/2025/03/15/part-002.parquet
// /events/2025/03/15/part-003.parquet
// Total: ~384 MB for a busy day, 3 files to manage

// Avoid:
// /events/2025/03/15/00001.json (1 KB each, thousands of files)
// /events/2025/march.parquet    (no day-level pruning possible)

Using ISODate Partition Attributes

For S3 paths that encode timestamps in ISO format (e.g., 2025-03-15), use the ISODate partition attribute type. This lets you filter with MongoDB date comparison operators ($gte, $lt, $lte) against the partition attribute, and Data Federation will correctly prune files outside the requested date range.

// Path: /events/{date ISODate}/data.parquet
// e.g.: /events/2025-03-15/data.parquet

// Query with ISODate range — triggers pruning
db.events.find({
  date: {
    $gte: ISODate('2025-03-01'),
    $lt:  ISODate('2025-04-01')
  }
})
// Only reads /events/2025-03-*/ directories

Compacting and Re-Partitioning Files

Over time, streaming data pipelines often produce many small files (the 'small files problem'). To maintain query performance, periodically compact files: read all files in a partition and write them back as fewer, larger files. This is typically done with Spark, AWS Glue, or a scheduled Atlas Function that reads and re-exports data.

// Compact using MongoDB Data Federation's own $out
// (Re-export a partition to a temporary location, then replace)
db.events_raw.aggregate([
  { $match: { year: 2024, month: 1 } },
  { $out: {
    s3: {
      bucket: 'mycompany-analytics-archive',
      region: 'us-east-1',
      filename: 'events/2024/1/compacted-{UUID()}.parquet',
      format: { name: 'parquet' }
    }
  }}
])

Exporting Data to S3 With $out

Data Federation supports an extended form of $out that writes aggregation results directly to S3 instead of an Atlas collection. This is used for ETL pipelines: read raw data from Atlas, transform it, and export the result to S3 in an optimised format (Parquet, BSON) for future federated queries or downstream analytics tools.

// Export to S3 in Parquet format from Atlas
db.orders.aggregate([
  { $match: { year: 2025, month: 3 } },
  { $project: { customerId: 1, amount: 1, region: 1, status: 1 } },
  { $out: {
    s3: {
      bucket: 'mycompany-analytics-archive',
      region: 'us-east-1',
      filename: 'orders/2025/3/data.parquet',
      format: { name: 'parquet', maxFileSize: '128MB' }
    }
  }}
])

Combining Partition Pruning and Column Pruning

Maximum performance comes from combining both pruning strategies: partition pruning (time/entity attributes in the path skips whole files) and column pruning (early $project skips unneeded Parquet columns within each file). Together they can reduce I/O by 99%+ compared to a naive full-scan query on an unpartitioned, uncompressed dataset.

// Combined: partition pruning + column pruning
db.events.aggregate([
  // Partition pruning: only reads March 2025 S3 files
  { $match: { year: 2025, month: 3, eventType: 'purchase' } },
  // Column pruning: only reads userId, amount columns from Parquet
  { $project: { userId: 1, amount: 1, _id: 0 } },
  { $group: { _id: '$userId', total: { $sum: '$amount' } } },
  { $sort: { total: -1 } },
  { $limit: 100 }
])

Monitoring Partition Pruning Effectiveness

In the Atlas Data Federation query history, each query shows the number of files matched versus the number of files actually read. If matched >> read, partition pruning is working well. If matched ≈ read, your partition structure does not align with your query patterns and you should redesign the partition layout or add more partition attributes.

// Check execution stats to verify pruning
db.events.explain().aggregate([
  { $match: { year: 2025, month: 3 } },
  { $group: { _id: '$eventType', count: { $sum: 1 } } }
])
// In the explain output, look for:
// 'partitionsProcessed': 31   (one per day in March)
// 'partitionsTotal': 365      (total days in the year)
// Effective pruning: 31/365 files scanned

Partition Design: A Practical Checklist

When designing your S3 partition structure: 1) Identify the fields your queries filter most frequently — those become partition attributes. 2) Put the highest-cardinality, most-frequently-filtered field at the top of the path hierarchy. 3) Target 64–256 MB per file. 4) Use Parquet format for analytical workloads to also benefit from column pruning. 5) Include a date-based partition (year/month/day) for time-series data.

// Example: well-designed partition hierarchy for event data
// Prioritises: region (high selectivity), then year, month, day
// Path: /events/{region string}/{year int}/{month int}/{day int}/

// Query: EU events in March 2025
// Reads ONLY: /events/EU/2025/3/
// Skips: all other regions and all other months/years
db.events.find({ region: 'EU', year: 2025, month: 3, eventType: 'purchase' })

Quick Check

Test your understanding of MongoDB & NoSQL Databases concepts from this lesson.

Lesson Recap

In this lesson you learned: partition attributes encode metadata in S3 paths so the query engine can skip non-matching files (partition pruning), combining partition pruning with column pruning in Parquet delivers the greatest I/O reduction, and aim for 64-256 MB file sizes to balance per-file API overhead with pruning granularity. Next up we explore Atlas Triggers for reacting to database events.

เริ่มต้นได้ฟรี

เรียนรู้ JavaScript ด้วย AI tutor — ฟรี

เขียนและเรียกใช้โค้ดจริงในเบราว์เซอร์ของคุณ รับความช่วยเหลือทันทีจาก AI tutor 24/7 และเรียนรู้ต่อจากที่คุณหยุดบนเว็บหรือในแอป

คอร์ส
30
บทเรียน
120

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

บทเรียน “การแบ่งพาร์ทิชันข้อมูล S3 เพื่อประสิทธิภาพการค้นหา” ฟรีหรือไม่

ใช่ — ข้อความเต็มของ “การแบ่งพาร์ทิชันข้อมูล S3 เพื่อประสิทธิภาพการค้นหา” ฟรีให้อ่านที่นี่บนเว็บ เพื่อปฏิบัติแบบโต้ตอบ (ตัวแก้ไขโค้ดในตัวและติวเตอร์ AI ตลอด 24/7) และปลดล็อคส่วนที่เหลือของคอร์ส MongoDB Academy ให้อัปเกรดเป็น CoddyKit PRO คอร์ส MongoDB Academy มีบทเรียนทั้งหมด 4 บทเรียน

คุณจะเรียนรู้อะไรในบทเรียน “การแบ่งพาร์ทิชันข้อมูล S3 เพื่อประสิทธิภาพการค้นหา”

ผู้เรียนจะกำหนดแอตทริบิวต์พาร์ทิชันบนเส้นทาง S3 เพื่อให้ Data Federation ตัดไฟล์ที่ไม่เกี่ยวข้องออกและส่งมอบการค้นหาเชิงวิเคราะห์ที่รวดเร็ว คุณปฏิบัติ MongoDB Academy ด้วยโค้ดที่ใช้งานได้จริงที่คุณเรียกใช้โดยตรงในเบราว์เซอร์ และติวเตอร์ AI ตลอด 24/7 ตอบคำถามของคุณขณะที่คุณไปผ่านบทเรียน

คุณต้องมีประสบการณ์ก่อนที่จะเริ่มเรียน MongoDB Academy หรือไม่

ไม่จำเป็นต้องมีประสบการณ์มาก่อน MongoDB Academy บน CoddyKit ออกแบบมาสำหรับผู้เริ่มต้นไปจนถึงผู้เรียนขั้นสูง คุณสามารถเริ่มต้นที่นี่หรือเริ่มจากตัวแรกและเรียนด้วยความเร็วของคุณเอง นี่คือบทเรียนที่ 4 จากทั้งหมด 4 บทเรียน

บทเรียน “การแบ่งพาร์ทิชันข้อมูล S3 เพื่อประสิทธิภาพการค้นหา” ใช้เวลานานแค่ไหน

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

ฉันเขียนและรันโค้ดในบทเรียน MongoDB Academy นี้ได้ไหม

ได้ บทเรียน MongoDB Academy ทุกบทมีตัวแก้ไขโค้ดในตัว คุณจึงเขียนและรันโค้ดจริงได้เลยในเบราว์เซอร์ และได้รับข้อเสนอแนะจาก AI ในทันที — ไม่ต้องติดตั้งในเครื่องของคุณ

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

  1. Atlas Data Federation คืออะไร
  2. การแมปแหล่งข้อมูล S3 และ Atlas ไปยังเนมสเปซเสมือน
  3. การเรียกใช้ไปป์ไลน์การรวมข้อมูลข้ามแหล่งที่มา
  4. การแบ่งพาร์ทิชันข้อมูล S3 เพื่อประสิทธิภาพการค้นหา
← กลับไปที่ MongoDB Academy