Overview
Batch operations allow you to perform multiple similar queries in a single function call, significantly improving performance. Instead of making individual calls for each query, batch methods process multiple queries internally with optimized database access.Available Batch Methods
The Aggregate component provides three batch operations:countBatch()- Count items for multiple boundssumBatch()- Sum items for multiple boundsatBatch()- Get items at multiple offsets
Performance Benefits
Batch operations provide substantial performance improvements:- Reduced function call overhead: One call instead of many separate invocations
- Optimized database access: Fewer database round trips and better internal optimization
- Improved transaction efficiency: Smaller transaction scope with less potential for conflicts
countBatch()
Count items across multiple bounds in a single call:Basic Usage
With Namespaces
Example: Count by Score Ranges
sumBatch()
Sum values across multiple bounds:atBatch()
Retrieve items at multiple offsets efficiently:With Bounds
Negative Offsets
Real-World Example: User Statistics Dashboard
When to Use Batch Operations
Good Use Cases
- Fetching statistics for multiple users/groups at once
- Calculating percentiles or distribution histograms
- Loading data for dashboards with multiple aggregates
- Comparing metrics across different time ranges or categories
When Individual Calls Are Fine
- Single query that doesn’t repeat
- Queries with different namespaces that could conflict
- Interactive queries where results are needed one at a time
Combining with Regular Operations
You can mix batch and regular operations:Best Practices
- Use batch operations for similar queries: Best when queries differ only in bounds or namespace
- Maintain query order: Results are returned in the same order as the input array
- Handle empty results: Check for null/undefined when using batch operations
- Combine with Promise.all: Batch different operation types in parallel
Next Steps
- Learn about Keeping Data in Sync
- Understand Table Aggregates
- Explore Direct Aggregates