market=SUI, hour=12 scans a single shard (~600KB) instead of the full dataset (~30GB).
Shard Key Types
OnDB supports three sharding strategies that can be combined in a hierarchy:| Type | Description | Use Case |
|---|---|---|
discrete | Exact value partitioning. Each unique value gets its own shard. | Markets, regions, tenant IDs |
time_range | Time-based bucketing with configurable granularity (minute, hour, day, week, month). | Logs, events, time-series data |
hash_distributed | Even distribution across a fixed number of buckets via hashing. | High-cardinality fields, write-heavy workloads |
Setting Up Sharding
With syncCollection (Recommended)
syncCollection creates or updates a collection’s indexes and sharding configuration in a single call. This is the recommended approach for new collections.
sharding_configured field that confirms whether sharding was applied.
With setupSharding (Existing Collection)
UsesetupSharding to add sharding to a collection that already exists and has data.
Shard Key Configuration
ShardingStrategy
ShardKey
Examples
Time-Series Data
Partition event logs by day for efficient date-range queries:Multi-Tenant Data
Isolate tenant data into discrete shards:High-Volume Writes
Distribute writes evenly across hash buckets to avoid hotspots:Query Considerations
Whenenforce_in_queries is set to true, every query against the collection must include all shard key fields. This prevents full-collection scans and ensures queries hit only the relevant shards.
enforce_in_queries: false. Be aware that queries without shard key filters will scan all shards.